.net 中datagridview 中如何删除多行,同时删除数据库

private void btn_sc_Click(object sender, EventArgs e)
{
int id = Convert.ToInt32(dg_zcdw.CurrentRow.Cells[0].Value);
DialogResult da = MessageBox.Show("是否删除?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
if (da == DialogResult.Yes)
{
int result = AssetStateManage.DeleteAssetState(id); //删除的方法我是调用的三层中写好的删除方法
if (result > 0)
{
System.Windows.Forms.MessageBox.Show("删除成功!");
}
}
}

这个是我写的代码 只能删除一行 如何在这个基础上删除多行同时数据库也删除,
三层中的删除方法:
public static int DeleteAssetState(int asa)
{
string sql = "delete AssetState where A_zcztid=" + asa;
return DBHelpSQL.ExecuteCommand(sql);

}
高手指教呀

要批量删除的话,一般就是设置一下gridview的样式,在每一行上放入一个复选框,选中哪个复选框就删除哪行。
protected void btnAll_Click(object sender, EventArgs e)
{
//全选用
int rowchk = grdMyCart.Rows.Count;
if (btnAll.Text == "全 选")
{
btnAll.Text = "全不选";
for (int i = 0; i < rowchk; i++)
{
CheckBox chk = (CheckBox)grdMyCart.Rows[i].FindControl("chSeclect");
chk.Checked = true;
}
}
else
{
btnAll.Text = "全 选";
for (int i = 0; i < rowchk; i++)
{
CheckBox chk = (CheckBox)grdMyCart.Rows[i].FindControl("chSeclect");
chk.Checked = false;
}
}
}
protected void btnDel_Click(object sender, EventArgs e)
{
//////-------删除产品信息---------
ArrayList rowCel = new ArrayList();
int rowCount = grdMyCart.Rows.Count;
//循环得到选中产品的编号
for (int i = 0; i < rowCount; i++)
{
CheckBox chDel = (CheckBox)grdMyCart.Rows[i].FindControl("chSeclect");
if (chDel.Checked == true)
{
rowCel.Add(grdMyCart.Rows[i].Cells[1].Text);
}
}
//调用数据访问类的删除方法
if (rowCel.Count > 0)
{
int resault = Delcardata(rowCel);
lblMessage.Text = "成功删除了 " + resault + " 条记录!";
}
else
{
Response.Write("<script language=\"javascript\">window.alert(\"请选择要删除的书籍!\")</script>");
}
//重新读取购物车信息
displayData();
}

这是我以前找到过的代码片段,你看看
温馨提示:答案为网友推荐,仅供参考
第1个回答  2011-09-14
我也刚做完这样类似的一个功能
我的第一列存储ID,以下的代码,表面看是删除一条记录,其实是可以删除所有选中的数据行:

private void DataMonitorPoint_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e)
{

try
{
//ISDelete = true;
string PointID = e.Row.Cells[0].Value.ToString();//捕捉选中的ID,
string SqlDelete = "Delete * from DTS_MONITOR_POINT where ID=" + PointID;
DbHelperOleDb.ExecuteSql(SqlDelete);

}
catch
{

}
}
第2个回答  2011-09-14
dg_zcdw.SelectedRows[0].Cell[0].Value
dg_zcdw.SelectedRows[1].Cell[1].Value

public static int DeleteAssetState(List<int> asa)
{
string ids = string.Join(",",asa);
string sql = "delete AssetState where A_zcztid IN (" + ids + ")";
return DBHelpSQL.ExecuteCommand(sql);

}