http://dotnet.aspx.cc/Exam/DataGridCheckBox/DataGridCheckBox.aspxhttp://dotnet.aspx.cc/ShowDetail.aspx?id=FF130C7F-3650-4DA6-8943-8AA4AF3E3459执行删除操作即可

解决方案 »

  1.   

    我现实现了一个写法,但是我觉得效率比较低,每删除n条数据的时候就要打开关闭数据库n次,有没有更高效的代码啊?
    附:
    我刚写的实现删除的代码
    private void Button6_Click(object sender, System.EventArgs e)
    {

      string ID=null;
       foreach(DataGridItem item in DataGrid1.Items) 
         { CheckBox txtID = (CheckBox)item.FindControl("chkDelServer");  if (txtID.Checked == true)
    {

    ID=txtID.Text;
    string SQL = "DELETE FROM News_News  WHERE News_ID="+ ID +"";
            string connstr=System.Configuration.ConfigurationSettings.AppSettings["strConnection"].ToString();

             SqlConnection conn=new SqlConnection(connstr);
    SqlCommand comm=new SqlCommand(SQL,conn); conn.Open();
    comm.ExecuteNonQuery();
    conn.Close();
    }

         } Response.Redirect("xxxx.aspx");//返回}
      

  2.   

    private void Delete_Click(object sender, System.EventArgs e)            
    {
    string dgIDs = "";
                bool BxsChkd = false; 
                foreach (DataGridItem i in MyDataGrid.Items) 
                {
                    CheckBox deleteChkBxItem = (CheckBox) i.FindControl ("DeleteThis");
                    if (deleteChkBxItem.Checked) 
                    {
                        BxsChkd = true;
                        dgIDs += ((Label) i.FindControl ("StoreID")).Text.ToString() + ",";
                    }
                }
                string deleteSQL = "DELETE from Stores WHERE stor_id IN (" + dgIDs.Substring (0, dgIDs.LastIndexOf (",")) + ")";            if (BxsChkd == true) 
                { 
                    
                    try 
                    {
                        //SqlHelper.ExecuteNonQuery (objConnect, CommandType.Text, deleteSQL);//根据delete from youtable where id in()来执行批删除
                        OutputMsg.InnerHtml+="删除"+dgIDs.ToString().Trim()+"成功";
                        OutputMsg.Style["color"] = "#3366cc";
                    } 
                    catch (SqlException err) 
                    { 
                        OutputMsg.InnerHtml += err.Message.ToString(); 
                        OutputMsg.Style["color"] = "#6699cc";
                    }
                    BindData();
    }
      

  3.   

    DataGrid  CheckBox 删除源码下载 :
    http://www.cnblogs.com/smilnet/archive/2004/07/17/25150.aspx