我正在使用带有 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.**