asp.net gridview 删除CheckBox选中记录无反应(页面只是刷新一下,没有删除)
删除按钮代码string sql = "";
string connString = ConfigurationManager.ConnectionStrings["sjlj"].ToString();
SqlConnection conn = new SqlConnection(connString);
for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
{
CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
if (cbox.Checked == true)
{
sql = "delete from rkb where id='" + GridView1.DataKeys[i].Value + "'";
conn.Open();
SqlCommand com = new SqlCommand(sql, conn);
com.ExecuteNonQuery();
conn.close();
}
}
sjdq();
数据库读取代码
public void sjdq()
{
string connString = ConfigurationManager.ConnectionStrings["sjlj"].ToString();
SqlConnection con = new SqlConnection(connString);
con.Open();
string sql = "select * from rkb order by 日期";
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = new SqlCommand(sql, con);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.AllowPaging = true;
GridView1.PageSize = 5;
GridView1.DataKeyNames = new string[] { "id" };
GridView1.DataBind();
con.Close();
}