以下这段,是对查询出来的数据表的单行数据,通过点击每行后的删除图标,进行删除,请问能否实现针对选中的多行数据,进行批量删除,若是这样,请问应该怎么实现;
if (e.ColumnIndex == dataGridView1.Columns["DeleteColumn"].Index)
{
SqlConnection conn = new SqlConnection("server=192.168.100.247;database=Whmesinfo;user=sa;password=whyy@2021");
conn.Open();
for (int i = 0; i < dataGridView1.SelectedRows.Count; i++)
{
string id = dataGridView1.SelectedRows[i].Cells["id"].Value.ToString();
string datadel = "delete from w_proyirate where id = '" + id.ToString() + "'";
SqlCommand comm = new SqlCommand(datadel, conn);
int x = comm.ExecuteNonQuery();
//执行判断
if (x == 0)
{
MessageBox.Show("删除失败");
}
else
{
MessageBox.Show("删除成功");//增加了提示,与下面的即时刷新不见的效果;
dataGridView1.Rows.Remove(dataGridView1.SelectedRows[i]);
}
}
}