GridView实现全选批量删除功能
protected void Button3_Click(object sender, EventArgs e)//实现多选删除功能
{
foreach (GridViewRow gvr in GridView1.Rows)
{
CheckBox ch = (CheckBox)gvr.FindControl( "ItemCheckBox ");
if (ch.Checked)
{
string connStr =
ConfigurationManager.ConnectionStrings "rsc "].ConnectionString;
SqlConnection conn = new SqlConnection(connStr);
conn.Open();
SqlCommand comm = new SqlCommand();
SqlDataAdapter sqlad = new SqlDataAdapter();
comm.Connection = conn;
sqlad.SelectCommand = comm;
string sql;
sql = "delete from rsc where au_id= 'GridView1.DataKeys[gvr.DataItemIndex].Value.ToString() ' ";
comm.CommandText = sql;
comm.ExecuteNonQuery();
}
}
}
报错为:將 varchar 數值 'GridView1.DataKeys[gvr.DataItemIndex].Value.ToString() ' 轉換成資料型別為 int 的資料行語法錯誤
------解决方案--------------------sql = "delete from rsc where au_id= "+GridView1.DataKeys[gvr.DataItemIndex].Value.ToString();
------解决方案--------------------> > >
sql = "delete from rsc where au_id= " + GridView1.DataKeys[gvr.DataItemIndex].Value.ToString();