我有一个数据网格,并添加了复选框以在需要时删除一些行。但是我使用的代码不是100%正确的,因为当我检查1或2行然后单击我创建的删除按钮时,它会删除我的所有行,而不仅仅是我选择的那些..有什么问题?
BindingSource myDataListBindingSource;
myDataListBindingSource = new BindingSource();
dataGrid_reservations.DataSource = myDataListBindingSource;
gestion_hotel bdd1 = new gestion_hotel();
DataGridViewRow row = new DataGridViewRow();
for (int i = 0; i < dataGrid_reservations.Rows.Count; i++)
{
row = dataGrid_reservations.Rows[i];
if (Convert.ToBoolean(row.Cells[0].Value) == true)
{
int id = Convert.ToInt16(row.Cells[1].Value);
dataGrid_reservations.Rows.Remove(row);
i--;
}
}
答案 0 :(得分:0)
这是检查复选框是否已选中的方法
foreach (DataGridViewRow row in dataGridView1.Rows)
{
DataGridViewCheckBoxCell chk = (DataGridViewCheckBoxCell)row.Cells[1];
if (chk.Value == chk.TrueValue)
{
//it is checked
}
else
{
//it isnt checked
}
}