asp.net gridview 删除CheckBox选中记录无反应(页面只是刷新一下,没有删除)
删除按钮代码stringsql="";stringconnString=ConfigurationManager.ConnectionStrings["sjlj"].To...
删除按钮代码
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();
}
}
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();
} 展开
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();
}
}
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();
} 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询