Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# C WPF SQL Server:如何清除组合框项_C#_Sql Server_Wpf - Fatal编程技术网

C# C WPF SQL Server:如何清除组合框项

C# C WPF SQL Server:如何清除组合框项,c#,sql-server,wpf,C#,Sql Server,Wpf,我有两个组合框:在CBO1中,项目是手动添加的。当用户从cbo 1中选择一个值时,它应该动态地填充第二个组合框项目列表。但当用户从cbo 1中选择不同的项目时,cbo 2项目列表不会清除,并且随着用户从cbo 1中选择不同的项目,其列表会一次又一次地填充。当我添加combobox.Items.Clear方法时,代码会崩溃 第一个组合框项是通过xml代码添加的,如下所示,而第二个组合框项是通过代码从SQL Server表中动态获取的 xaml 而不是使用cboModel.Items.Clear;,

我有两个组合框:在CBO1中,项目是手动添加的。当用户从cbo 1中选择一个值时,它应该动态地填充第二个组合框项目列表。但当用户从cbo 1中选择不同的项目时,cbo 2项目列表不会清除,并且随着用户从cbo 1中选择不同的项目,其列表会一次又一次地填充。当我添加combobox.Items.Clear方法时,代码会崩溃


第一个组合框项是通过xml代码添加的,如下所示,而第二个组合框项是通过代码从SQL Server表中动态获取的

xaml

而不是使用cboModel.Items.Clear;,将其设置为新的空集合/列表

cboModel.Items=新的可观察集合;或
cboModel.Items=新列表

如何添加项?第一个组合框项是通过如下所示的xml代码添加的,而第二个组合框项是通过上述代码从SQL Server表中动态获取其项的。您是否可以在之前执行.items.Clear从selectionchange填充它?在foreach语句的正上方,尝试cboModel.Items.Clear;您还应该了解executeScalar方法,该方法用于在查询中返回单个结果,这比使用适配器填充表的开销要小得多。如果您只是从dt1.Rows[0].Cells[0]中获取信息,那么您不应该需要一个循环来从datatable中获取该信息-您不应该将您的SQL语句连接在一起-使用参数化查询来避免SQL注入-签出请修改我的代码并发送给我,因为我是C语言的新手,不知道如何使用上述解决方案您在添加cboModel.Items.Clear时说过;在项目中,代码会中断。尝试使用我上面的一种方法,就像您这样做时一样:DataTableDT1=新DataTableProduct;你能告诉我你的comboModel是什么数据类型吗?
<ComboBox x:Name="comboCat" Grid.ColumnSpan="3" Grid.Column="5" Margin="10,2,20,10" Grid.Row="7" VerticalContentAlignment="Center" SelectionChanged="comboCat_SelectionChanged"> <ComboBoxItem Content="AC"/> <ComboBoxItem Content="Fridge"/> <ComboBoxItem Content="LED TV"/>
private void comboCat_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    ComboBoxItem cbi = (ComboBoxItem)comboCat.SelectedItem;
    comboCategorySelection = cbi.Content.ToString();

    string connection = "Data Source = MCR-CDA-6003\\MSSQLSERVERNEW; Initial Catalog = 99Bell-ERP; Integrated Security = True";

    using (var cc = new SqlConnection(connection))
    {                
        string CmdString = "select ModelNo from Product where CompanyID = 'PEL' and Category = '" + comboCategorySelection + "' group by Category, ModelNo";
        MessageBox.Show(CmdString);

        SqlCommand cmd = new SqlCommand(CmdString, con);

        SqlDataAdapter sda = new SqlDataAdapter(cmd);

        DataTable dt1 = new DataTable("Product");
        sda.Fill(dt1);

        foreach (DataRow dr in dt1.Rows)
        {
            comboModel.Items.Add(dr["ModelNo"].ToString());
        }
    }
}

private void comboModel_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    string connection = "Data Source = MCR-CDA-6003\\MSSQLSERVERNEW; Initial Catalog = 99Bell-ERP; Integrated Security = True";

    using (var cc = new SqlConnection(connection))
    {
        string CmdString = "select ProductID from Product where ModelNo = '" + comboModel.SelectedItem.ToString() + "'";
        MessageBox.Show(CmdString);

        SqlCommand cmd = new SqlCommand(CmdString, con);

        DataTable dt1 = new DataTable("Product");
        SqlDataAdapter sda = new SqlDataAdapter(cmd);
        sda.Fill(dt1);

        foreach (DataRow dr in dt1.Rows)
        {
            txtProductID.Text = dr["ProductID"].ToString();
        }
    }
}