日期:2014-05-18  浏览次数:20529 次

请教,在DATAGRID中如何进行多个数据同时删除的操作?
在DATAGRID中添加摸版列,放入CHECKBOX。然后在DATAGRID外再放一个BUTTON,当点BUTTON的时候,删除CHECKBOX所选择的一个或多个记录,该怎么控制呢?


谢谢

------解决方案--------------------
void Button_Click(...)
{
string result = " ";
foreach(DataGridItem item in GridName.Items)
{
CheckBox chk = (CheckBox)item.FindControl(checkboxid);
if(chk.checked)
result += string.Format( " '{0} ', ",item.cells[keyIndex].Text);
}
if(result != " ")
result = result.SubString(0,result.Length-1);
string sql = string.Format( "delete from TableName where key in ({0}) ",result);

....
}
------解决方案--------------------
private void btnDelete_Click(object sender, System.EventArgs e)
{
string whereClause = string.Empty;
foreach(DataGridItem dgi in DataGrid1.Items)
{
CheckBox chk = (CheckBox)dgi.FindControl( "chkSelect ");
if(chk != null)
{
if(chk.Checked)
{
if(whereClause == string.Empty)
whereClause += " ' " + DataGrid1.DataKeys[dgi.ItemIndex].ToString() + " ' ";
else
whereClause += ", ' " + DataGrid1.DataKeys[dgi.ItemIndex].ToString() + " ' ";
}
}
}
if(whereClause != string.Empty)
{
SqlConnection cn = new SqlConnection( "server=.;uid=sa;pwd=;database=pubs ");
string strSQL = "delete from authors where au_id in ( " + whereClause + ") ";
SqlCommand cmd = new SqlCommand(strSQL, cn);
cn.Open();
cmd.ExecuteNonQuery();
cn.Close();
}
BindGrid();
}