Not able to make cascading comboboxes work
Asked Answered
S

1

0

Here is my code to create cascading comboboxes. I am trying to populate Family Combobox(ComboBox2) based on the value selected for Segment Name(combox1).I am able to populate the first Combo with a static resource but my second one shows blank. I am calling a method called "FillComboBoxFamilyData(SegmentCode)" on the selection change event of first combobox.

XAML CODE:

<Grid Height="142" HorizontalAlignment="Left" Margin="49,113,0,0" Name="grid3"     VerticalAlignment="Top" Width="904">
                <ComboBox Height="23" HorizontalAlignment="Left" Margin="35,26,0,0"    Name="comboBox1" VerticalAlignment="Top" Width="205" ItemsSource="{Binding Source={StaticResource tblSegmentViewSource}}"  DisplayMemberPath="Segment Name" SelectedValuePath="Segment Code" SelectionChanged="comboBox1_SelectionChanged"/>
                <ComboBox Height="23" HorizontalAlignment="Right" Margin="0,26,395,0"    Name="comboBox2" VerticalAlignment="Top" Width="205" />


  <Window.Resources>
  <CollectionViewSource x:Key="tblSegmentViewSource" Source="{Binding Path=TblSegment,    Source={StaticResource brickDataset}}" />
    <CollectionViewSource x:Key="tblFamilyViewSource" Source="{Binding Path=TblFamily,  Source={StaticResource brickDataset}}" />

*BrickDataSet is the main dataset I am pulling the tables from.*

C#:

    private void comboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        MessageBox.Show(comboBox1.SelectedValue.ToString());
        SegmentCode = Convert.ToInt32(comboBox1.SelectedValue.ToString());
        FillComboBoxFamilyData(SegmentCode);
    }

    public void FillComboBoxFamilyData(int Segment_Code)
    {
        string connString =
            "Data Source=.\\SQLEXPRESS;AttachDbFilename=C:\\Documents and Settings\\dchaman\\My Documents\\PDRT.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True ";

        SqlConnection con = new SqlConnection(connString);
        con.Open();
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = con;
        cmd.CommandType = System.Data.CommandType.Text;
        cmd.CommandText =
            "SELECT  TblFamily.[Family Name],TblFamily.[Family Code] FROM TblFamily WHERE (TblFamily.[Segment Code] = @SegmentCode)";

        cmd.Parameters.AddWithValue("@SegmentCode", Segment_Code);

        DataSet objDs = new DataSet();
        SqlDataAdapter dAdapter = new SqlDataAdapter();
        dAdapter.SelectCommand = cmd;
        dAdapter.Fill(objDs);
        con.Close();
        if (objDs.Tables[0].Rows.Count > 0)
        {
            MessageBox.Show("Here I am");
            comboBox2.DataContext = objDs.Tables;
            comboBox2.Items.Insert(0, "--Select Family Name--");
            comboBox2.DisplayMemberPath = "Family Name";
            comboBox2.SelectedValue = "Family Code";
        }
    }    

I am banging my head with the table right now.Please Save me!

Stow answered 7/2, 2011 at 19:54 Comment(1)
So, you're getting the MessageBox, but the combo doesn't have the info? I see that you're setting combobBox2's DataContext, but you haven't bound anything to that context.Pawnbroker
L
0

You aren't setting the comboBox2's ItemsSource. The DataContext will simply effect all binding statements on it. If you set the ItemsSource to the correct table instead of the DataContext, it should get you on the right path:

if (objDs.Tables[0].Rows.Count > 0)
{
    MessageBox.Show("Here I am");
    comboBox2.ItemsSource = ((IListSource)objDs.Tables[0]).GetList(); // set the ItemsSource instead of the DataContext
    comboBox2.DisplayMemberPath = "Family Name";
    comboBox2.SelectedValue = "Family Code";
}

Note that when you set the ItemsSource, the line that inserts text into the Items property will fail, as you can not use both the ItemsSource and the Items together.

Landlocked answered 7/2, 2011 at 20:11 Comment(5)
Thanks a lot for the reply. What do u mean when you say set itemsource to 'correct Table' ,what table do I set it to, you mean the 'ObjDs' DataSet ?Stow
I meant you would assign it the table you want in your DataSet. I updated the answer to give you an example.Landlocked
Thanks.It just displays the first row when I do ------comboBox2.ItemsSource = objDs.Tables--; and throws an exception when write -- comboBox2.ItemsSource = objDs.Tables[0]---; The exception is 'An explicit conversion exists,you can not convert system.data.datatable into system.Collections.IEnumerable'Stow
Sorry, I forgot that a DataTable doesn't implement IEnumerable. You have to cast the table to an IListSource, and call GetList (since the table implements the interface explicitly). The Binding engine does this behind the scenes for you when you use a CollectionViewSource in XAML.Landlocked
I wonder if I could make them work using a user interface as I did in WinFormsStow

© 2022 - 2024 — McMap. All rights reserved.