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...
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
RemoveHandler combo.SelectedIndexChanged, AddressOf combo_SelectedIndexChangedACCT
AddHandler combo.SelectedIndexChanged, AddressOf combo_SelectedIndexChangedACCT
End If
End If
If dgvAcctSelect.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
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.