Click here to Skip to main content
15,891,607 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
the Problem is
I have a combobox
in Datagridview
and is filled with items in the form load
based on the selection I am filling the next combo
which is also inside the datagridview.

the problem is that when I am selecting the first combo
on the basis of selection the second combo is getting filled

but on the next row
when i am selecting another item in the first combo
all the combo on second coloumn is getting filled with the item based on it.

how to rectify this
please assist

VB
Public Class Form3
    Dim WithEvents cmb1 As ComboBox

    Private Sub DataGridView1_EditingControlShowing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
        If DataGridView1.SelectedCells(0).ColumnIndex = 0 Then
            If TypeOf e.Control Is ComboBox Then
                cmb1 = DirectCast(e.Control, ComboBox)

            End If
        End If
        
    End Sub

Private Sub cmb1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmb1.SelectedIndexChanged
        Column2.Items.Clear()
        If (DataGridView1.Rows(DataGridView1.CurrentRow.Index).Cells(0).Selected) Then
            'MessageBox.Show("as")
            If cmb1.Text = "ABCD" Then
                Column2.Items.Add("asdasdasD")

            ElseIf cmb1.Text = "EFGH" Then
                Column2.Items.Add("D")

            End If


        End If
    End Sub
 Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        With Column1
            .Items.Add("ABCD")
            .Items.Add("EFGH")
        End With
    End Sub
Posted
Updated 8-Jun-12 23:59pm
v2
Comments
VJ Reddy 9-Jun-12 9:10am    
Thank you for accepting the solution :)

1 solution

The reason may be that the DataSource is common for all rows for the DataGridViewComboBoxColumn. So, when the items of the second DataGridViewComboBoxColumn are populated the same items remain for other columns.

An alternative to solve this problem as follows:
VB
Public Class Form1
    Dim countries As New DataTable()
    Dim cities As New DataTable()
    Dim eventData As New DataTable()
    Dim dgv As New DataGridView()
    Dim citiesView As DataView

    Private Sub Form1_Load(ByVal sender As System.Object, _
            ByVal e As System.EventArgs) Handles MyBase.Load
        countries.Columns.Add("CountryId", GetType(String), Nothing)
        countries.Columns.Add("Country", GetType(String), Nothing)
        countries.Rows.Add("1", "India")
        countries.Rows.Add("2", "USA")
        countries.Rows.Add("3", "Canada")
        countries.Rows.Add("4", "Australia")

        cities.Columns.Add("CityId", GetType(String), Nothing)
        cities.Columns.Add("CountryId", GetType(String), Nothing)
        cities.Columns.Add("City", GetType(String), Nothing)
        cities.Rows.Add("11", "1", "Hyderabad")
        cities.Rows.Add("12", "1", "Bangalore")
        cities.Rows.Add("13", "1", "Chennai")
        cities.Rows.Add("21", "2", "New York")
        cities.Rows.Add("22", "2", "Washington")
        cities.Rows.Add("23", "2", "Dallas")
        cities.Rows.Add("31", "3", "Toronto")
        cities.Rows.Add("32", "3", "Ottawa")
        cities.Rows.Add("41", "4", "Canberra")
        cities.Rows.Add("42", "4", "Brisbane")
        cities.Rows.Add("43", "4", "Perth")

        citiesView = New DataView(cities)

        eventData.Columns.Add("Event", GetType(String), Nothing)
        eventData.Columns.Add("CountryId", GetType(String), Nothing)
        eventData.Columns.Add("CityId", GetType(String), Nothing)
        eventData.Rows.Add("Annual Meeting", "1", "12")
        eventData.Rows.Add("Seminar 1", "3", "31")
        eventData.Rows.Add("Product Demo", "2", "22")

        dgv.Dock = DockStyle.Fill
        dgv.AutoGenerateColumns = False
        dgv.DataSource = eventData

        Dim eventCol As New DataGridViewTextBoxColumn()
        eventCol.Name = "Event"
        eventCol.DataPropertyName = "Event"
        dgv.Columns.Add(eventCol)

        Dim countryCol As New DataGridViewComboBoxColumn()
        countryCol.DataSource = countries.DefaultView
        countryCol.DataPropertyName = "CountryId"
        countryCol.DisplayMember = "Country"
        countryCol.ValueMember = "CountryId"
        countryCol.Name = "CountryId"
        dgv.Columns.Add(countryCol)

        Dim cityCol As New DataGridViewComboBoxColumn()
        cityCol.DataSource = cities.DefaultView
        cityCol.DataPropertyName = "CityId"
        cityCol.DisplayMember = "City"
        cityCol.ValueMember = "CityId"
        cityCol.Name = "CityId"
        dgv.Columns.Add(cityCol)

        AddHandler dgv.EditingControlShowing, AddressOf dgv_EditingControlShowing
        dgv.EditMode = DataGridViewEditMode.EditOnEnter

        Controls.Add(dgv)
        Width = 450
    End Sub
    Sub dgv_EditingControlShowing(ByVal sender As Object, _
            ByVal args As DataGridViewEditingControlShowingEventArgs)
        Dim dgv = CType(sender, DataGridView)
        If dgv.CurrentCell.ColumnIndex <> dgv.Columns("CityId").Index Then
            Return
        End If
        If dgv.CurrentRow.Cells("CountryId").Value Is DBNull.Value Then
            Return
        End If
        Dim countryId As String = dgv.CurrentRow.Cells("CountryId").Value.ToString()

        citiesView.RowFilter = String.Format("CountryId='{0}'", countryId)
        Dim dgvcbEdit = TryCast(dgv.EditingControl, DataGridViewComboBoxEditingControl)
        If dgvcbEdit Is Nothing Then
            Return
        End If
        dgvcbEdit.DataSource = citiesView
        dgvcbEdit.SelectedValue = dgv.CurrentCell.Value.ToString()
    End Sub
End Class


To run the above code, create a Windows Forms application, place above code inside Form1.vb file and run the application.
 
Share this answer
 
v3
Comments
Karwa_Vivek 9-Jun-12 7:40am    
your code is nit clear.
please clarify.will i do this in form load Event.
is not possible.please assist
HOW TO PLACE THIS CODE
VJ Reddy 9-Jun-12 8:13am    
It seems there is an issue with anonymous method used for the event handler.
I have modified the code. Place the above code inside Form1.vb file of Windows Forms application and run the program

Thank you :)
Manas Bhardwaj 9-Jun-12 19:11pm    
Very well +5!
VJ Reddy 9-Jun-12 20:35pm    
Thank you, Manas :)
But I think the vote is not updated.
Manas Bhardwaj 9-Jun-12 21:32pm    
Oops!!! Thanks. Good you pointed out and please do so if you miss again :)

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