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:
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.