在我的查看项目表单中,当我选择索引0时,我有两个项目“Packet and Piece”的comboBox它应该检索数据包数据或选择索引1来检索Piece数据..会发生什么每次不同的选择都是清除数据网格视图行和列但是它将先前选择的列放在新的那个..我的代码中的问题在哪里? ..问候
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if(comboBox1.SelectedIndex==0)
{
viewItemsdgv.DataSource = null;
viewItemsdgv.Rows.Clear();
viewItemsdgv.Columns.Clear();
dt.Clear();
da = new SqlDataAdapter("select itmBar as Barcode,itmTrdName as 'Trading name' ,itmScName as 'Scintfic name',pack as 'Pills in packet',tab as 'Pills in table',exdate as 'Expire date',packQuant as 'Packet quantity',tabQuant as 'Table quantity' from item where buyDflt !='buyDfltPiecerdb'", cn);
da.Fill(dt);
viewItemsdgv.DataSource = dt;
}
if (comboBox1.SelectedIndex == 1)
{
viewItemsdgv.DataSource = null;
viewItemsdgv.Rows.Clear();
viewItemsdgv.Columns.Clear();
dt.Clear();
da = new SqlDataAdapter("select itmBar as Barcode,itmTrdName as 'Trading name' ,pieceQuant as 'Pieces quantity',exdate as 'Expire date' from item where buyDflt='buyDfltPiecerdb'", cn);
da.Fill(dt);
viewItemsdgv.DataSource = dt;
}
}
答案 0 :(得分:1)
您正在删除DataGridView的列,您还需要删除datatable(dt)的列。
添加
dt.Columns.Clear();
之前
dt.Clear();