winform中DataGridView全选右键删除的问题
删除多条数据,可是程序每次只获得了最后一条选中数据,int selectNum = this.dataGridView1.SelectedRows.Count;
选中多行,selectNum 还是为1,不知道是那里出了错误,这是代码!
private void tsmDel_Click(object sender, EventArgs e)
{
int count = 0;
for (int i = 0; i < dataGridView1.RowCount; i++)
{
if (dataGridView1.Rows[i].Cells["全选"].EditedFormattedValue.ToString() == "True")
{
count++;
}
}
if (count == 0)
{
MessageBox.Show("请至少选择一条数据!", "提示");
return;
}else
{
int selectNum = this.dataGridView1.SelectedRows.Count;
if (MessageBox.Show(this, "共选择" + count + "条,你要删除这些数据吗?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information).ToString() == "Yes")
{
//得到选中的行数
for (int i = 0; i < selectNum; i++)
{
string uid = Convert.ToString(dataGridView1.SelectedRows[i].Cells[2].Value.ToString());
//执行删除
SpecialPermitService.Del(uid);
}
}else
{
return;
}
MessageBox.Show("删除成功");
DataBind();
}
}
------解决方案--------------------
你设置的只是Cells["全选"]的值,并不是多选,for循环中加上一句
dataGridView1.Rows[i].Selected = true;