Note that there are some explanatory texts on larger screens.

plurals
  1. POadding 2 combobox columns in datagridview
    primarykey
    data
    text
    <p>I am using a datagridview with 2 combobox columns to show account no and description in vb.net 2005.</p> <pre><code>my query = "select acctno, acctdesc from mytable union select ' ','' from mytable" (why union? - so that once user selects acct no, can deselect) </code></pre> <p>I am filling ds1 and ds2 with the query above using dataadapter.</p> <pre><code>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 </code></pre> <p>This is giving Cell value not valid error.</p> <hr> <p>If anybody just want to try, just take datagridview with 2 comboboxcolumns. Show any 2 columns like id and desc from any tbale. And try to select id and show desc automatically in othet column. Vice versa </p> <hr> <pre><code> 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 " &amp; _ "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 " &amp; _ "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.** </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload