我正在使用带有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值无效错误。
如果有人想尝试,只需使用2个comboboxcolumns的datagridview。从任何tbale显示任何2列,如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.**
答案 0 :(得分: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不会导致您获得Cell值错误。我希望这有帮助。检查你的代码。