C# 组合框值不断添加到列表中
我有一张有两个组合框的表格 combobox1 unitupc combobox2生产线 首先,加载unitupc,然后为每个选中的unitupc填充combobox2。我遇到的问题是,对于用户选择的每个unitupc,前面的值都存储在combobox2中,并且列表不断添加,如何在每次选择unitupc时清除Combox2并重新加载 下面是我所说的问题的表格图片: 添加编辑代码C# 组合框值不断添加到列表中,c#,combobox,C#,Combobox,我有一张有两个组合框的表格 combobox1 unitupc combobox2生产线 首先,加载unitupc,然后为每个选中的unitupc填充combobox2。我遇到的问题是,对于用户选择的每个unitupc,前面的值都存储在combobox2中,并且列表不断添加,如何在每次选择unitupc时清除Combox2并重新加载 下面是我所说的问题的表格图片: 添加编辑代码 private void DimensionSelection_Load(object sender, Even
private void DimensionSelection_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'CorsicanaNetWeightDataSet10.Net_Weight_Master_Data_Report' table. You can move, or remove it, as needed.
// TODO: This line of code loads data into the 'corsicanaNetWeightDataSet9.ProductionLine' table. You can move, or remove it, as needed.
prodline = new productweightdataset();
// TODO: This line of code loads data into the 'corsicanaNetWeightDataSet3.ProductionLine' table. You can move, or remove it, as needed.
//this.productionLineTableAdapter.Fill(this.corsicanaNetWeightDataSet3.ProductionLine,comboBox3.Text.ToString());
// TODO: This line of code loads data into the 'corsicanaNetWeightDataSet2.ItemDescription' table. You can move, or remove it, as needed.
//this.itemDescriptionTableAdapter.Fill(this.corsicanaNetWeightDataSet2.ItemDescription);
loadprod();
this.reportViewer1.RefreshReport();
reportViewer1.Visible = false;
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
//if (comboBox1.SelectedIndex > -1)
//{
// button1.Enabled = true;
//}
Loadproduction();
comboBox2.Refresh();
}
private void loadprod()
{
try
{
using (MSSQL.SqlConnection connection = new MSSQL.SqlConnection(constr))
{
connection.Open();
using (MSSQL.SqlCommand command = new MSSQL.SqlCommand("SELECT [Unit UPC Base Item] as Unitupc, [Item Description] AS ItemDescription FROM ItemDesc", connection))
{
{
MSSQL.SqlDataAdapter myadapter = new System.Data.SqlClient.SqlDataAdapter();
myadapter.SelectCommand = command;
myadapter.Fill(prodline, "DataTable1");
}
}
//fill drop down
comboBox1.DataSource = prodline.DataTable1;
comboBox1.ValueMember = "ItemDescription";
comboBox1.DisplayMember = "ItemDescription";
comboBox3.DataSource = prodline.DataTable1;
comboBox3.ValueMember = "Unitupc";
comboBox3.DisplayMember = "Unitupc";
if (comboBox1.Items.Count > 0)
{
comboBox1.SelectedIndex = 0;
Loadproduction();
}
}
}
catch (Exception) { /*Handle error*/ }
}
//private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
//{
// this.productionLineTableAdapter1.Fill(this.corsicanaNetWeightDataSet9.ProductionLine, comboBox3.Text.ToString());
//}
private void Loadproduction()
{
if (comboBox1.SelectedValue.ToString().Trim().Length > 0)
{
try
{
using (MSSQL.SqlConnection connection = new MSSQL.SqlConnection(constr))
{
connection.Open();
using (MSSQL.SqlCommand command = new MSSQL.SqlCommand("SELECT TOP (100) PERCENT dbo.[Production Lines].[Production Line Description] AS prodline FROM dbo.[Production Lines] LEFT OUTER JOIN dbo.[Net Weight Master Data] ON dbo.[Production Lines].[Production Line] = dbo.[Net Weight Master Data].[Production Line] RIGHT OUTER JOIN dbo.ItemDesc ON dbo.[Net Weight Master Data].[Unit UPC Base Item] = dbo.ItemDesc.[Unit UPC Base Item] WHERE (dbo.ItemDesc.[Unit UPC Base Item] = '0001') GROUP BY dbo.[Production Lines].[Production Line], dbo.[Production Lines].[Production Line Description], dbo.ItemDesc.[Unit UPC Base Item] ORDER BY dbo.ItemDesc.[Unit UPC Base Item]", connection))
{
MSSQL.SqlParameter myparam = new MSSQL.SqlParameter();
myparam.Direction = ParameterDirection.Input;
myparam.ParameterName = "@unitupc";
myparam.SqlDbType = SqlDbType.NVarChar;
myparam.Size = 50;
myparam.Value = comboBox3.Text;
command.Parameters.Add(myparam);
MSSQL.SqlDataAdapter myadapter = new System.Data.SqlClient.SqlDataAdapter();
myadapter.SelectCommand = command;
myadapter.Fill(prodline, "DataTable2");
comboBox2.DataSource = prodline.DataTable2;
comboBox2.DisplayMember = "prodline";
}
}
}
catch (Exception) { /*Handle error*/ }
}
}
您是否已经尝试过:
ComboBox2.Items.Clear()
编辑:我同意普拉西的答案。查看更多信息。在绑定新值之前,必须清除Combobox的数据源。使用“清除”按建议将数据表清除为@LarsTech。查看我的编辑
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
comboBox2.DataSource = null;
Loadproduction();
comboBox2.Refresh();
}
编辑:
您一直在填充数据表,因此请先清除它:
prodLine.DataTable2.Rows.Clear();
myadapter.Fill(prodline, "DataTable2");
这是什么语言?你的代码在哪里?@LynnCrumbling抱歉,我以为我添加了代码,我已经在上面的问题中包含了代码。请帮忙!!当ComboBox使用数据源时,您提供的链接的@j0h4nn3s的可能重复项将不起作用。@LarsTech还有一个答案是考虑数据源我尝试了您的解决方案我遇到了以下错误,无法隐式地将类型“void”转换为“object”@HackMaster您准确地键入了吗?尝试
prodLine[“DataTable2”].Rows.Clear()代码>取而代之。在调用该行之前,请确保该表存在;它不工作,我仍然得到无法隐式转换'void'到对象plz帮助@黑客大师那不是我写的。清除表中的行,填充表(如果必须),然后设置数据源。您的数据源行不应更改。将您的解决方案与prasy的解决方案结合使用,非常感谢您的帮助。我得到这个错误是因为在解决方案中有prodLine.DataTable2.Rows.Clear();但它必须是prodline:)它仍然在做同样的事情thing@HackMaster试着用我的答案来回答。在绑定新值之前,请清除数据表并清除数据源组合解决方案有效,但我使用了清除而不是重置,因为重置使我的组合框2变为空,感谢您的帮助,非常感谢!!!!。
prodLine.DataTable2.Rows.Clear();
myadapter.Fill(prodline, "DataTable2");