Click here to Skip to main content
15,173,299 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Respected Sir,

In my Windows Application using Vb.net, In the datagridview i have two different DataGridViewComboBoxColumn

I have attached dataset to 1st DataGridViewComboBoxColumn..
now as per selection item from this combo i want to attach database to 2nd DataGridViewComboBoxColumn


example:
Following are 2 DatagridviewCombobox columns in DataGridView
CountryCombobox         CityCombobox (Selected Item Is 'India' From                                        
                                                       1st combo that is
                                                       'CountryCombobox')
---------------        ------------------  
US                     Delhi
UK                     Mumbai  
India                  Kolkota
                       Banglore


Please help me...

Thank you...
Posted
Updated 4-Dec-19 17:12pm
v2

use dataGridView_CurrentCellDirtyStateChanged event for achieve this
check currentcell's column is your first combo's column
then as per value change datasource of second combobox cell of that current row,

e.g.

VB
Private Sub dgv1_CurrentCellDirtyStateChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles dgv1.CurrentCellDirtyStateChanged
        Try
            If (dgv1.IsCurrentCellDirty) Then
                dgv1.CommitEdit(DataGridViewDataErrorContexts.Commit)
                If dgv1.CurrentCell.ColumnIndex = 1 Then
                    If dgv1.CurrentCell.Value <> 0 Then
                        Dim t = DirectCast(dgv1.Rows(dgv1.CurrentCell.RowIndex).Cells(3), DataGridViewComboBoxCell)
                        t.DataSource = DBGetData("select col1,col2 from tbl1 where col3=" & dgv1.CurrentCell.Value)
                        t.DisplayMember = "col1"
                        t.ValueMember = "col2"
                        t.Value = 0
                    End If
                End If
            End If
        Catch
        End Try
    End Sub


Happy Coding!
:)
   
v3
Comments
Sunil Bansode 29-Aug-12 4:06am
   
Thanks a lot...
Its working...
Aarti Meswania 29-Aug-12 4:07am
   
most welcome :)
You need to reselect the data, otherwise the two controls will synchronise. You can make a copy of the data source rather than query the DB twice.
   
Comments
Sunil Bansode 29-Aug-12 4:07am
   
Thanks for advice
If you are using the Dirtied method then its better to use the ColumIndex rather that the cell because if you are adding new Rows the cell value is nothing has the application will throw an error. Here is my Modified solution based on Solution 2
VB
Private Sub dgTrans_CurrentCellDirtyStateChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles dgTrans.CurrentCellDirtyStateChanged
       Try
           If (dgTrans.IsCurrentCellDirty) Then
               dgTrans.CommitEdit(DataGridViewDataErrorContexts.Commit)
               If dgTrans.CurrentCell.ColumnIndex = 1 Then
                   If dgTrans.CurrentCell.Value <> 0 Then
                       Dim t As DataGridViewComboBoxColumn = dgTrans.Columns(3)
                       t.DataSource = DBGetData("select col1,col2 from tbl1 where col3=" & dgTrans.CurrentCell.Value)
                       t.DisplayMember = "col1"
                       t.ValueMember = "col2"
                       t.Value = 0
                   End If
               End If
           End If
       Catch
       End Try
   End Sub
   

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