在線等,,以下多選刪除報錯,
將 varchar 數值 'GridView1.DataKeys[gvr.DataItemIndex].Value' 轉換成資料型別為 int 的資料行語法錯誤。
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string connStr = ConfigurationManager.ConnectionStrings["BTS"].ConnectionString;
SqlConnection conn = new SqlConnection(connStr);
conn.Open();
DataSet ds = new DataSet();
SqlCommand comm = new SqlCommand();
SqlDataAdapter sqlad = new SqlDataAdapter();
comm.Connection = conn;
comm.CommandText = "select * from bts";
sqlad.SelectCommand = comm;
//Response.Write(sqlad.SelectCommand.CommandText);
sqlad.Fill(ds, "bts");
GridView1.DataSource = ds.Tables["bts"];
GridView1.DataBind();
}
}
protected void Button3_Click(object sender, EventArgs e)
{
foreach (GridViewRow gvr in GridView1.Rows)
{
CheckBox ch = (CheckBox)gvr.FindControl("ItemCheckBox");
if (ch.Checked)
{
//Ret2.Text += "<li>GridView1 您选择的是(键值):" + GridView1.DataKeys[gvr.DataItemIndex].Value.ToString();
string connStr = ConfigurationManager.ConnectionStrings["BTS"].ConnectionString;
SqlConnection conn = new SqlConnection(connStr);
conn.Open();
DataSet ds = new DataSet();
SqlCommand comm = new SqlCommand();
SqlDataAdapter sqlad = new SqlDataAdapter();
string sql;
sql = "delete from bts where au_id= 'GridView1.DataKeys[gvr.DataItemIndex].Value'";
comm.Connection = conn;
comm.CommandText = sql;
comm.ExecuteNonQuery();
}
}
}
protected void CheckAll(object sender, EventArgs e)
{
CheckBox cbx = (CheckBox)sender;
foreach (GridViewRow gvr in GridView1.Rows)
{
CheckBox ch = (CheckBox)gvr.FindControl("ItemCheckBox");
ch.Checked = cbx.Checked;
}
}