Click here to Skip to main content
15,891,513 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am using visual studio 2005 vb.net windows application.

In a datagridview i have 2 combobox columns and want to use selectedindexchanged event for them.

my code which is not working perfect...
VB
With dgvAcctSelect.Columns

           Dim cmbCol1 As New DataGridViewComboBoxColumn
           Dim ds1 As DataSet
           Dim da1 As Data.SqlClient.SqlDataAdapter

           ds1 = New DataSet
           da1 = New Data.SqlClient.SqlDataAdapter("SELECT ACCTNO,ACCTDESC FROM ACCTTABLE union " & _
           "select ' ' as ACCTNO,'' as ACCTDESC from ACCTTABLE order by ACCTNO", sqlCon)
           ds1.Clear()
           da1.Fill(ds1, "tab1")
           Try
               If ds1 Is Nothing = False Then
                   cmbCol1.DataSource = ds1.Tables("tab1").DefaultView
                   cmbCol1.DisplayMember = "ACCTNO"
                   cmbCol1.ValueMember = "ACCTNO"
               End If
           Catch ex As Exception
           End Try
           cmbCol1.DisplayStyle = DataGridViewComboBoxDisplayStyle.ComboBox
           cmbCol1.HeaderText = "Account No"
           cmbCol1.Name = "ACCTNO"
           cmbCol1.Width = 300
           cmbCol1.AutoComplete = True
           .Add(cmbCol1)

           Dim cmbCol2 As New DataGridViewComboBoxColumn
           Try
               If ds1 Is Nothing = False Then
                   cmbCol2.DataSource = ds1.Tables("tab1").DefaultView
                   cmbCol2.DisplayMember = "ACCTDESC"
                   cmbCol2.ValueMember = "ACCTNO"
               End If
           Catch ex As Exception
           End Try
           cmbCol2.DisplayStyle = DataGridViewComboBoxDisplayStyle.ComboBox
           cmbCol2.HeaderText = "Account Description"
           cmbCol2.Name = "ACCTDESC"
           cmbCol2.Width = 330
           cmbCol2.AutoComplete = True
           cmbCol2.Visible = True
           .Add(cmbCol2)

       End With

Private Sub dgvAcctSelect_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles dgvAcctSelect.EditingControlShowing
       Try
           If dgvAcctSelect.CurrentCell.OwningColumn.Name = "ACCTNO" Then
               Dim combo As ComboBox = TryCast(e.Control, ComboBox)
               If combo IsNot Nothing Then
                   'If combo.Text <> "" And combo.Text <> "System.Data.DataRowView" Then
                   ' Remove an existing event-handler, if present
                   RemoveHandler combo.SelectedIndexChanged, AddressOf combo_SelectedIndexChangedACCT
                   ' Add the event handler
                   AddHandler combo.SelectedIndexChanged, AddressOf combo_SelectedIndexChangedACCT
                   'End If
               End If
           End If

           If dgvAcctSelect.CurrentCell.OwningColumn.Name = "ACCTDESC" Then
               Dim combo As ComboBox = TryCast(e.Control, ComboBox)
               If combo IsNot Nothing Then
                   'If combo.Text <> "" And combo.Text <> "System.Data.DataRowView" Then
                   ' Remove an existing event-handler, if present
                   RemoveHandler combo.SelectedIndexChanged, AddressOf combo_SelectedIndexChangedDESC
                   ' Add the event handler
                   AddHandler combo.SelectedIndexChanged, AddressOf combo_SelectedIndexChangedDESC
                   'End If
               End If
           End If
       Catch ex As Exception
       End Try
   End Sub

   Private Sub combo_SelectedIndexChangedACCT(ByVal sender As Object, ByVal e As EventArgs)
       Try
           If dgvAcctSelect.CurrentCell.OwningColumn.Name = "ACCTNO" Then
               Dim cb As ComboBox = DirectCast(sender, ComboBox)
               If cb.Text <> "" And cb.Text <> "System.Data.DataRowView" Then
                   dgvAcctSelect.CurrentRow.Cells(1).Value = cb.SelectedValue
               End If
           End If
       Catch ex As Exception
       End Try
   End Sub

   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)
               If cb.Text <> "" And cb.Text <> "System.Data.DataRowView" Then
                   dgvAcctSelect.CurrentRow.Cells(0).Value = cb.SelectedValue
               End If
           End If
       Catch ex As Exception
       End Try
   End Sub


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

(If i select acct no, desc should be seen in other column for that acct no and if i select acct desc, acct no should be seen in other column for that acct desc. This is what i want to achieve )

(If possible i want to make comboboxcolumns auto complete)

Above code is working but i have to click on combobox 3 times. 3rd time it will show me the list of items to select. Then after i select item it will show corresponding value in other combobox column.

for 2nd row if i click 1st time nothing happens. if i click 2nd time it shows previous row value for that column and corresponding column shows value "System.Data.DataRowView". 3rd time it show list and then if i select other item it shows corresponding value in other combobox column correctly.

What i am doing wrong ??

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

RemoveHandler combo.validated, AddressOf combo_SelectedIndexChangedDESC
' Add the event handler
AddHandler combo.validated, AddressOf combo_SelectedIndexChangedDESC

Validated event is working fine but only thing u cant see corresponding value in other column immediately. You have to click somewhere else which doesnt look/feel as good as selectedindexchanged event.

thanks...Please suggest any solution.
Posted
v2
Comments
Ganesh KP 11-Nov-13 22:42pm    
Hi Harshada, the code is very difficult to read and answer to your question. There are no comments mentioned any where in the code, try to separate each method and also mention comments for each method, so that is will be easier for every one to look at and to answer. Hope u understands.
Thava Rajan 11-Nov-13 23:30pm    
where did you (in which event)use the with section of your code may i know and instead of using cell(1) or Cell(0) try to use the name of the column then only it is more readable

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