Click here to Skip to main content
15,884,177 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.

XML
<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


VB
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
Posted

1 solution

Hi Ahmad,

First lets translate your need to a pattern:

Basically you need to change districts by the selected province => Master-Detail Pattern.

Province is the Master and the districts are the Details based on the Master.

So now we can see the we need Master-Detail Pattern.

But what is the best way to do that? Well I can see that you are using DataGrid and build your columns one by one... So lets think about it... Why shouldn't we build a simple Data-Template? And why should we use a DataGrid?

In my article:

How to: Use the Master-Detail Pattern with WPF Hierarchical DataTemplate

You can see that I don't need to use DataGrid for a grid-like UI structure. It's much more easy to use ItemsControl With Data-Template:

XML
<ItemsControl ItemsSource="{Binding Items}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal" MinWidth="320">
                <TextBlock Width="130" Text="{Binding Name}"/>
                <ComboBox Width="130" ItemsSource="{Binding Employees}" SelectedIndex="{Binding EmployeesSelectedIndex}" />
                <ComboBox Width="130" ItemsSource="{Binding Subordinates}" />
            </StackPanel>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>
 
Share this answer
 
v2

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