c#删除checkbox所选中的行
时间: 2024-01-23 21:03:19 浏览: 29
假设你使用的是 Windows Forms 应用程序,你可以按照以下步骤删除 DataGridView 中所选中的行:
1. 在 DataGridView 中添加一个列,类型为 DataGridViewCheckBoxColumn,用于显示复选框。
2. 在 DataGridView 的 CellContentClick 事件中添加以下代码:
```
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == yourCheckBoxColumn.Index && e.RowIndex >= 0)
{
DataGridViewRow row = dataGridView1.Rows[e.RowIndex];
if ((bool)row.Cells[yourCheckBoxColumn.Index].FormattedValue)
{
dataGridView1.Rows.Remove(row);
}
}
}
```
在这段代码中,yourCheckBoxColumn 是你刚刚添加的 DataGridViewCheckBoxColumn 列的名称,dataGridView1 是你的 DataGridView 控件的名称。
以上代码的作用是:当用户单击复选框时,检查该行的复选框是否被选中,如果选中,则将该行从 DataGridView 中删除。
希望这可以帮助到你!