我有一个有两个组合框的表格。
combobox1 unitupc
combobox2生产线
首先,unitupc被加载,然后对于所选择的每个unitupc,组装了combobox2。我遇到的问题是,对于用户选择的每个unitupc,先前的值存储在combobox2中并且列表不断添加,每次选择unitupc时如何清除组合框并重新加载?
以下是我正在谈论的问题表单的图片:
已添加修改代码
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*/ }
}
}
答案 0 :(得分:2)
您继续填写DataTable,请先清除它:
prodLine.DataTable2.Rows.Clear();
myadapter.Fill(prodline, "DataTable2");
答案 1 :(得分:1)
在绑定新值之前,您必须清除Combobox的DataSource。使用Clear将DataTable清除为@LarsTech,如建议的那样。看我的编辑
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
comboBox2.DataSource = null;
Loadproduction();
comboBox2.Refresh();
}
编辑:
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;
prodline.DataTable2.Clear(); //or u can use prodline.DataTable2.Reset() --Reset removes all data, indexes, relations, and columns of the table
myadapter.Fill(prodline, "DataTable2");
comboBox2.DataSource = prodline.DataTable2;
comboBox2.DisplayMember = "prodline";
}
}
}
catch (Exception) { /*Handle error*/ }
}
}
答案 2 :(得分:0)
您是否已尝试过此操作:ComboBox2.Items.Clear()
?