我有一个名为"myTable"的数据表,它与一个名为"dgv"的DataGridView绑定。DataGridView "dgv"有一列复选框。我的目标是在按钮事件中删除选中的行。当然,数据表会被更新。
现在我的代码只能删除一行,而不能删除多行。
谢谢帮助。
谢谢帮助。
private void btnDel_Click(object sender, EventArgs e)
{
try
{
if (dgv.RowCount>0)
{
foreach (DataGridViewRow row in dgv.Rows)
{
DataGridViewCheckBoxCell check = row.Cells[0] as DataGridViewCheckBoxCell;
if (check.Value != null)
{
if ((bool)check.Value)
{
DataRowView currentDataRowView = (DataRowView)dgv.CurrentRow.DataBoundItem;
DataRow dataRow = currentDataRowView.Row;
int n = dgv.CurrentRow.Index;
int intID = Convert.ToInt32(dgv.Rows[n].Cells[0].Value);
myTable.Rows.Remove(dataRow);
dgv.DataSource = myTable;
Int32 intVal = Convert.ToInt32(row.Cells[1].Value);
if (intVal == intID)
{
check.Value = null;
}
}
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}