WinForm DataGridView根据选中的行多删
private void btndel_Click(object sender, EventArgs e)
{
int count = 0;
//获取选中的行数
count = this.dataGridView1.SelectedRows.Count;
if (count == 0)
{
MessageBox.Show("至少选择一项!!", "提示");
return;
}
DialogResult dr = MessageBox.Show("共"+count+"条,确定要删除吗?","警告",MessageBoxButtons.YesNo,MessageBoxIcon.Information);
if(dr==DialogResult.Yes)
{
string str = "";
//循环选中的行并获取想要的值
foreach (DataGridViewRow row in dataGridView1.SelectedRows)
{
str += row.Cells[0].Value.ToString()+",";
}
if (str.Length > 0)
{
str = str.Substring(0, str.Length - 1);
}
StudentBLL bll = new StudentBLL();
//调用删除的方法
if (bll.DelStudent(str) > 0)
{
MessageBox.Show("删除成功", "提示");
StudentShow();//刷新数据
}
else
{
return;
}
}