无法使级联组合框工作

发布于 2024-10-16 04:47:45 字数 2798 浏览 6 评论 0原文

这是我创建级联组合框的代码。我正在尝试根据为段名称 (combox1) 选择的值来填充系列组合框 (ComboBox2)。我能够使用静态资源填充第一个组合,但第二个组合显示为空白。 我在第一个组合框的选择更改事件上调用名为“FillComboBoxFamilyData(SegmentCode)”的方法。

XAML 代码:

<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 是我从中提取表格的主要数据集。*

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";
        }
    }    

我现在正在用表格撞我的头。请救救我!强>

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

墨落画卷 2024-10-23 04:47:45

您没有设置comboBox2 的ItemsSourceDataContext 将简单地影响其上的所有绑定语句。如果您将 ItemsSource 设置为正确的表而不是 DataContext,它应该会让您走上正确的路径:

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";
}

请注意,当您设置 ItemsSource 时>,将文本插入 Items 属性的行将会失败,因为您不能同时使用 ItemsSourceItems

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文