Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
i am tryign to add 2 combobox column in datagridview in 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 by choosing ' ' value)

i am using same query for both datasets ds1 and ds2 using data adapter

HTML
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 -

HTML
Private Sub dgv_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles dgv.EditingControlShowing

        Try

'handler for selected index changed events for acctno and desc columns

            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 SELECT CORRESPONDING ACCTDESC 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 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
Now also if i select acct no or desc it gives me cell value not valid error.

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

just take any 2 columns like id and desc in datagridview in 2 comboboxcolumns from any tbale. if u select id then desc should be displayed automaticlly and if u select desc, acct no should be displayed automaticlly. Try to do this.

trying to do selectedIndexchanged event for combobox in datagridview.

--------------------------------------------------------------------------------
Posted
Updated 3-Jul-12 17:16pm
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900