今天在做一个界面时,想要在datagridview中将选中行全删,找了一些资料但效果没有太好,便自己实现。
private void button4_Click(object sender, EventArgs e)/删除选中行/
{
int deleteindex = -1;
int deletecount = 0;
int maxRowIndex = -1;
int minRowIndex = 0x7FFFFFFF;
if(dataGridView1.SelectedCells.Count == 0)
{
return;
}
foreach (DataGridViewCell tmpCell in dataGridView1.SelectedCells)
{
if (deleteindex == -1)
{
deleteindex = tmpCell.RowIndex;
}
if (tmpCell.RowIndex > maxRowIndex)
{
maxRowIndex = tmpCell.RowIndex;
}
if (tmpCell.RowIndex < maxRowIndex)
{
minRowIndex = tmpCell.RowIndex;
}
}
deletecount = maxRowIndex - minRowIndex + 1;
for (int icnt = 0x0;icnt < deletecount;icnt ++)
{
dataGridView1.Rows.RemoveAt(deleteindex);
deleteindex = deleteindex - 1;
}
}
c#datagridview控件删除选中所有列
最新推荐文章于 2024-08-01 17:05:58 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)