我有 2 个组合框:在 中cbo #1,手动添加项目。当用户从 中选择一个值时cbo# 1,它应该动态填充第二个组合框项目列表。但是,当用户从 中选择不同的项目时cbo# 1,cbo# 2项目列表不会清除,并且随着用户从 中选择不同的项目,其列表会一次又一次地填充cbo# 1。当我添加combobox.Items.Clear()方法时,代码会崩溃。第一个组合框项目由“xml 代码”添加,如下所示,而第二个组合框通过代码从 SQL Server 表动态获取其项目。xml。<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"/>C#。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()); } }}
1 回答
- 1 回答
- 0 关注
- 172 浏览
添加回答
举报
0/150
提交
取消