dataGridView 中除了绑定DataGridViewTextBoxColumn外 还绑定了DataGridViewComboBoxColumn(以下称productName列)和一个DataGridViewButtonColumn(以下称oper列)
DataGridViewComboBoxColumn中初始时(状态1)是这样绑定的 ds = mp.Get_all();
productName.DataSource = ds.Tables[0];
productName.DisplayMember = "productName";
productName.ValueMember = "id";
有数据后(状态2)给它赋值是这样的: ds.Clear();
ds = ct.Get_product();
dataGridView1.DataSource = ds.Tables[0];
dataGridView1.Tag = "id";
dataGridView1.Columns["productName"].DataPropertyName = "productId";
dataGridView1.Columns["ncn"].DataPropertyName = "ncn";
dataGridView1.Columns["ncl"].DataPropertyName = "ncl";
dataGridView1.Columns["dwcpnh"].DataPropertyName = "dwcpnh"; 在 dataGridView1的CellContentClick事件里面写:
if (e.ColumnIndex == 4)//删除列在dataGridView1里面是第4列
{
try
{
dataGridView1.Rows.Remove(dataGridView1.CurrentRow);
}
catch
{
return;
}
}
问题是这个事件在DataGridView在(状态1)时能正常删除,而在(状态2)时没有任何反映 是怎么回事呀,郁闷死了
DataGridViewComboBoxColumn中初始时(状态1)是这样绑定的 ds = mp.Get_all();
productName.DataSource = ds.Tables[0];
productName.DisplayMember = "productName";
productName.ValueMember = "id";
有数据后(状态2)给它赋值是这样的: ds.Clear();
ds = ct.Get_product();
dataGridView1.DataSource = ds.Tables[0];
dataGridView1.Tag = "id";
dataGridView1.Columns["productName"].DataPropertyName = "productId";
dataGridView1.Columns["ncn"].DataPropertyName = "ncn";
dataGridView1.Columns["ncl"].DataPropertyName = "ncl";
dataGridView1.Columns["dwcpnh"].DataPropertyName = "dwcpnh"; 在 dataGridView1的CellContentClick事件里面写:
if (e.ColumnIndex == 4)//删除列在dataGridView1里面是第4列
{
try
{
dataGridView1.Rows.Remove(dataGridView1.CurrentRow);
}
catch
{
return;
}
}
问题是这个事件在DataGridView在(状态1)时能正常删除,而在(状态2)时没有任何反映 是怎么回事呀,郁闷死了
2。绑定的时候组合好数据,直接用DataSource = dateset。这样单调些,方便处理。
2、直接用DataSource = dateset怎么知道那个值绑定到哪里里?
1。如果是循环的ColumnIndex =0是正常的,否则就是事件的时候,状态被清空了,或则没有传过去。
2。dateset之前组织好,与DataSource 对应就可以了。
if (e.ColumnIndex == 4)//删除列在dataGridView1里面是第4列
{
try
{
dataGridView1.Rows.Remove(dataGridView1.CurrentRow);
}
catch
{
return;
}
}
这个问题您不用担心的,我知道列是从0开始的,从这里可以看到
dataGridView1.Columns["productName"].DataPropertyName = "productId";
dataGridView1.Columns["ncn"].DataPropertyName = "ncn";
dataGridView1.Columns["ncl"].DataPropertyName = "ncl";
dataGridView1.Columns["dwcpnh"].DataPropertyName = "dwcpnh";
我的数据列有4列,第5列才是那个操作按钮,用if (e.ColumnIndex == 4)的判断是没有错误的
productName.DisplayMember = "productName";
productName.ValueMember = "id"; 那个删除列是第4列
打开数据编辑的时候这样绑定ds = ct.Get_product();
dataGridView1.DataSource = ds.Tables[0];
dataGridView1.Tag = "id";
dataGridView1.Columns["productName"].DataPropertyName = "productId";
dataGridView1.Columns["ncn"].DataPropertyName = "ncn";
dataGridView1.Columns["ncl"].DataPropertyName = "ncl";
dataGridView1.Columns["dwcpnh"].DataPropertyName = "dwcpnh";
那个删除列就成了第1列了