且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从gridview中删除多行时获取错误

更新时间:2023-10-12 23:09:40

你的问题是那个你在一行的每次迭代中调用Binddata()。

完全移出Binddata()调用foreach。在完成处理之前,不要重新绑定数据。

Your issue is that you call Binddata() on every iteration of a row.
Move the Binddata() call out of the foreach completely. Don''t rebind your data until you are done processing.
foreach (GridViewRow row in GridView1.Rows)
{
   CheckBox chk = (CheckBox)row.FindControl("CheckBox1");
   if (chk.Checked)
   {
      int id = Convert.ToInt32("",GridView1.DataKeys[row.RowIndex].Value);
      SqlConnection sqlconn = new SqlConnection("server=.\\sqlexpress;database=practice;integrated security=true");
      SqlCommand sqlcomm = new SqlCommand("delete from prac where id='"+id+"'", sqlconn);
      sqlconn.Open();
      sqlcomm.ExecuteNonQuery();
      sqlconn.Close();
      Label1.Text = "Record Deleted";           
   }    
}
Binddata();