vb.net 2005でアカウント番号と説明を表示するために、2つのコンボボックス列を持つデータグリッドビューを使用しています.
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 を使用してください。任意のテーブルから id と desc のような任意の 2 列を表示します。そして、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.**