仅在另一个组合框选择后显示组合框中的更新值
only show the updated values in combo box after another combo box selection
当我 select 组合框 1 中的项目时,它显示组合框 2 中的项目。
当我 select 组合框 1 中的另一个项目时,它会在组合框 2 中同时显示先前结果和新结果的项目
我只想显示组合框 2 中的新项目。因为我 select 组合框 1 中的项目应该更新组合框 2 并删除以前的项目。
private void cb_oname_SelectedIndexChanged(object sender, EventArgs e)
{
SqlConnection sqlConnection = new SqlConnection(@"Data Source=.;Initial Catalog=Pizza Mania;Integrated Security=True");
{
SqlCommand sqlCmd2 = new SqlCommand("SELECT Product_category FROM Product2 where Product_Name='"+cb_oname.SelectedItem+"'", sqlConnection);
{
sqlConnection.Open();
SqlDataReader sqlrdr = sqlCmd2.ExecuteReader();
while (sqlrdr.Read())
{
cb_ocat.Items.add(sqlrdr["Product_category"].ToString());
cb_ocat.Update();
}
sqlConnection.Close();
}
}
}
您应该从第一个组合的 Items 集合中删除 selectedindex 处的项目。
请注意,我还更改了您的代码,以便在一次性对象和参数周围使用正确的 using 语句,而不是非常危险的字符串连接
private void cb_oname_SelectedIndexChanged(object sender, EventArgs e)
{
// Safety check, SelectedIndexChanged is called also when there is
// no item selected (see later)
if(cb_oname.SelectedIndex < 0)
return;
using(SqlConnection sqlConnection = new SqlConnection(.....))
using(SqlCommand sqlCmd2 = new SqlCommand(@"SELECT Product_category
FROM Product2 WHERE Product_Name=@name", sqlConnection))
{
sqlConnection.Open();
sqlCmd2.Parameters.Add("@name", SqlDbType.NVarChar).Value = cb_oname.SelectedItem;
using(SqlDataReader sqlrdr = sqlCmd2.ExecuteReader())
{
// Clear the previous items list
cb_ocat.Items.Clear();
while (sqlrdr.Read())
cb_ocat.Items.Add(sqlrdr["Product_category"].ToString());
}
}
// Remove from the Items collection, but it is not enough
cb_oname.Items.RemoveAt(cb_oname.SelectedIndex);
// Set the selectedindex to -1 so the current text in the combo is reset
cb_oname.SelectedIndex = -1;
}
当我 select 组合框 1 中的项目时,它显示组合框 2 中的项目。
当我 select 组合框 1 中的另一个项目时,它会在组合框 2 中同时显示先前结果和新结果的项目
我只想显示组合框 2 中的新项目。因为我 select 组合框 1 中的项目应该更新组合框 2 并删除以前的项目。
private void cb_oname_SelectedIndexChanged(object sender, EventArgs e)
{
SqlConnection sqlConnection = new SqlConnection(@"Data Source=.;Initial Catalog=Pizza Mania;Integrated Security=True");
{
SqlCommand sqlCmd2 = new SqlCommand("SELECT Product_category FROM Product2 where Product_Name='"+cb_oname.SelectedItem+"'", sqlConnection);
{
sqlConnection.Open();
SqlDataReader sqlrdr = sqlCmd2.ExecuteReader();
while (sqlrdr.Read())
{
cb_ocat.Items.add(sqlrdr["Product_category"].ToString());
cb_ocat.Update();
}
sqlConnection.Close();
}
}
}
您应该从第一个组合的 Items 集合中删除 selectedindex 处的项目。
请注意,我还更改了您的代码,以便在一次性对象和参数周围使用正确的 using 语句,而不是非常危险的字符串连接
private void cb_oname_SelectedIndexChanged(object sender, EventArgs e)
{
// Safety check, SelectedIndexChanged is called also when there is
// no item selected (see later)
if(cb_oname.SelectedIndex < 0)
return;
using(SqlConnection sqlConnection = new SqlConnection(.....))
using(SqlCommand sqlCmd2 = new SqlCommand(@"SELECT Product_category
FROM Product2 WHERE Product_Name=@name", sqlConnection))
{
sqlConnection.Open();
sqlCmd2.Parameters.Add("@name", SqlDbType.NVarChar).Value = cb_oname.SelectedItem;
using(SqlDataReader sqlrdr = sqlCmd2.ExecuteReader())
{
// Clear the previous items list
cb_ocat.Items.Clear();
while (sqlrdr.Read())
cb_ocat.Items.Add(sqlrdr["Product_category"].ToString());
}
}
// Remove from the Items collection, but it is not enough
cb_oname.Items.RemoveAt(cb_oname.SelectedIndex);
// Set the selectedindex to -1 so the current text in the combo is reset
cb_oname.SelectedIndex = -1;
}