I have a datagrid in Windows Presentation Foundation with two comboboxes. The first combobox is Provinces and the second is Districts. I have tables in SQL for provinces and districts with proper relations. I want to populate only the districts for the
selected provinces in the datagrid. I have looked at many articles on the web, but couldn't figure this out. I hope someone will help.
Here is the XAML for the datagrid.
And the code to populate the comboboxes is
Here is the XAML for the datagrid.
<DataGrid x:Name="dgBranches" Background="White" BorderBrush="#5ac7ff" HorizontalAlignment="Left" Margin="34,321,0,0" VerticalAlignment="Top" Height="120" Width="600" Grid.Row="1" Grid.Column="1" Grid.ColumnSpan="2" GridLinesVisibility="All" AutoGenerateColumns="False" IsSynchronizedWithCurrentItem="True"><DataGrid.Columns><DataGridCheckBoxColumn Binding="{Binding MainBranch}" ClipboardContentBinding="{x:Null}" Header="Main Branch" Width="100"/><DataGridComboBoxColumn SelectedValueBinding="{Binding ProvinceID}" x:Name="cbxBranch" DisplayMemberPath="Province" SelectedValuePath="ProvinceID" Header="Province" /><DataGridComboBoxColumn SelectedItemBinding="{Binding DistrictID}" x:Name="cbxDistrict" DisplayMemberPath="District" SelectedValuePath="DistrictID" Header="District" ItemsSource="{Binding Path=Districts}" /><DataGridTextColumn Binding="{Binding Village}" Header="Village"/><DataGridTextColumn Binding="{Binding Location}" Header="Location"/><DataGridTextColumn Binding="{Binding NameOfMarket}" Header="Market Name"/><DataGridTextColumn Binding="{Binding ShopNo}" Header="Shop Number"/></DataGrid.Columns></DataGrid>
And the code to populate the comboboxes is
Private Sub PopulateProvinces() Dim qry As String = "Select * From Provinces" Dim a As New SqlDataAdapter(qry, ConnectionRefDB.DBSql) Dim d As New DataTable a.Fill(d) Me.cbxIssuedFrom.ItemsSource = d.DefaultView Me.cbxBranch.ItemsSource = d.DefaultView End Sub Private Sub PopulateDistricts() Dim qry As String = "Select * From Districts" Dim a As New SqlDataAdapter(qry, ConnectionRefDB.DBSql) Dim d As New DataTable a.Fill(d) Me.cbxDistrict.ItemsSource = d.DefaultView End Sub