1

我正在使用带有 2 个组合框列的 datagridview 在 vb.net 2005 中显示帐号和描述。

my query = "select acctno, acctdesc from mytable union select ' ','' from mytable" (why union? - so that once user selects acct no, can deselect)

我正在使用 dataadapter 用上面的查询填充 ds1 和 ds2。

dim ds1 as new dataset
cmbcol1.datasource = ds1.tables(0).defaultview
cmbcol1.DisplayMember = "Acctno"
cmbCol1.ValueMember = "Acctno"

-----------------

dim ds2 as new dataset
cmbcol2.datasource = ds2.tables(0).defaultview
cmbcol2.DisplayMember = "Acctdesc"
cmbCol2.ValueMember = "Acctno"

-----------------

My code - 


    Private Sub dgvAcctSelect_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles dgvAcctSelect.EditingControlShowing

            Try
                If dgv.CurrentCell.OwningColumn.Name = "ACCTNO" Then
                    Dim combo As ComboBox = TryCast(e.Control, ComboBox)
                    If combo IsNot Nothing Then
                        RemoveHandler  combo.SelectedIndexChanged,  
                                   AddressOf  combo_SelectedIndexChangedACCT
                        AddHandler  combo.SelectedIndexChanged, 
                                   AddressOfcombo_SelectedIndexChangedACCT
                    End If
                End If

                If dgv.CurrentCell.OwningColumn.Name = "ACCTDESC" Then
                    Dim combo As ComboBox = TryCast(e.Control, ComboBox)
                    If combo IsNot Nothing Then
                       RemoveHandler combo.SelectedIndexChanged, AddressOf 
                               combo_SelectedIndexChangedDESC
                       AddHandler combo.SelectedIndexChanged, AddressOf 
                               combo_SelectedIndexChangedDESC
                    End If
                End If

            Catch ex As Exception
            End Try

        End Sub

' IF USER SELECTS ACCTNO THEN AUTOMATICALLY CORRESPONDING ACCTDESC should be get selected IN OTHER COLUMN


     Private Sub combo_SelectedIndexChangedACCT(ByVal sender As Object, ByVal e As EventArgs)
            Try
                If dgv.CurrentCell.OwningColumn.Name = "ACCTCNO" Then
                    Dim cb As ComboBox = DirectCast(sender, ComboBox)
                    dgv.CurrentRow.Cells(1).Value = cb.SelectedValue
                End If
            Catch ex As Exception
            End Try

        End Sub



' IF USER SELECTS ACCTDESC THEN AUTOMATICALLY SELECT CORRESPONDING ACCTNO should be get selected IN OTHER COLUMN

    Private Sub combo_SelectedIndexChangedDESC(ByVal sender As Object, ByVal e As EventArgs)
        Try
            If dgvAcctSelect.CurrentCell.OwningColumn.Name = "ACCTDESC" Then
                Dim cb As ComboBox = DirectCast(sender, ComboBox)
                dgv.CurrentRow.Cells(0).Value = cb.SelectedValue
            End If
        Catch ex As Exception
        End Try

    End Sub

这是给 Cell value not valid 错误。


如果有人只想尝试,只需使用带有 2 个组合框列的 datagridview。显示任何 2 列,如任何 tbale 中的 id 和 desc。并尝试在其他列中自动选择 id 并显示 desc。反之亦然


        Dim cmbCol1 As New DataGridViewComboBoxColumn
    Dim ds1 As DataSet
    Dim da1 As Data.SqlClient.SqlDataAdapter
    Try
        ds1 = New DataSet
        da1 = New Data.SqlClient.SqlDataAdapter("SELECT ACCTDESC ,ACCTNO FROM mytable union " & _
        "select ' ' as ACCTNO,'' as ACCTDESC from mytable order by ACCTno", sqlCon)
        ds1.Clear()
        da1.Fill(ds1, "ConnToSqlDrp1")
        If ds1 Is Nothing = False Then
            cmbCol1.DataSource = ds1.Tables("ConnToSqlDrp1").DefaultView
            cmbCol1.DisplayMember = "ACCTno"
            cmbCol1.ValueMember = "SACCTno"
        End If
    Catch ex As Exception
    End Try
    cmbCol1.DisplayStyle = DataGridViewComboBoxDisplayStyle.ComboBox
    cmbCol1.HeaderText = "Account No"
    cmbCol1.Name = "ACCTno"
    cmbCol1.Width = 150
    cmbCol1.AutoComplete = True
    .Add(cmbCol1)

    Dim cmbCol2 As New DataGridViewComboBoxColumn
    Dim ds2 As DataSet
    Dim da2 As Data.SqlClient.SqlDataAdapter
    Try
        ds2 = New DataSet
        da2 = New Data.SqlClient.SqlDataAdapter("SELECT ACCTNO,ACCTDESC FROM MYTABLE union " & _
       "select ' ' as ACCTNO,'' as ACCTDESC from MYTABLE order by ACCTDESC", sqlCon)
        ds2.Clear()
        da2.Fill(ds2, "ConnToSqlDrp2")
        If ds2 Is Nothing = False Then
            cmbCol2.DataSource = ds2.Tables("ConnToSqlDrp2").DefaultView
            cmbCol2.DisplayMember = "ACCTDESC"
            cmbCol2.ValueMember = "ACCTNO"
        End If
    Catch ex As Exception
    End Try
    cmbCol2.DisplayStyle = DataGridViewComboBoxDisplayStyle.ComboBox
    cmbCol2.HeaderText = "Consol Account Description"
    cmbCol2.Name = "ACCTDESC"
    cmbCol2.Width = 330
    cmbCol2.AutoComplete = True
    'cmbCol2.Visible = False
    .AdDcmbCol2)   



         **THIS IS MY CODE IN FORM LOAD EVENT SO IT SHOULD POPULATE ONLY ONCE.**
4

1 回答 1

0

这应该会给你一个 IndexOutOfBounds 异常。实际上它应该给它两次。

dim ds1 as new dataset
cmbcol1.datasource = ds1.tables(0).defaultview

dim ds2 as new dataset
cmbcol2.datasource = ds2.tables(0).defaultview

当您创建 Data.DataSet 的新实例时,tables 属性为空,因此没有实际填充数据集,我想知道您的应用程序如何不抛出异常?ds1.tables 数组中没有第 0 个表。如果这没有给您抛出异常,那么您的程序的执行逻辑就有缺陷。有些东西没有被调用,并且应该填充数据集。如果没有填充数据集并将其附加到组合框,这些组合框将为空,它们的 SelectedItem 什么都不是,这就是您收到单元格值错误的原因。我希望这有帮助。检查你的代码。

于 2012-07-03T04:18:02.743 回答