private void btnDEL_Click(object sender, System.EventArgs e) { int intCount=0; for(int i=0;i<this.dgShow.Items.Count;i++) { CheckBox chkCB = (CheckBox)dgShow.Items[i].FindControl("chkOPT"); //如果cbk被選中則執行操作 if(chkCB.Checked==true) { //得到選中行的第三列的值 string strLandCode = this.dgShow.Items[i].Cells[2].Text.Trim(); strSQL = "DELETE FROM AADA01TB WHERE LandCode='"+strLandCode+"'"; int j = BO.DeleteData(strSQL);//這個方法是自定義的方法,用來刪除數據的,你自己可以寫 if(j>0) { intCount++;//得到刪除的數據的數量 } } } if( intCount == dgShow.Items.Count && dgShow.CurrentPageIndex > 0 ) { //如果刪除數量與當前頁數據總和相等並且當前頁面索引號大於0 //讓DataGrid控件的頁面索號回到前一頁 -- dgShow.CurrentPageIndex; } Quary();//操作結束後重新顯示數據,保持數據的更新 }
好了,我改好了!这样就行了!try { conn=new SqlConnection(ConfigurationSettings.AppSettings["sqlConn"]); string myid=""; for(int i=0;i<DataGrid1.Items.Count;i++) { CheckBox cb=(CheckBox)this.DataGrid1.Items[i].FindControl("ck"); if(cb.Checked==true) { myid+=DataGrid1.DataKeys[i].ToString() +","; } } myid=myid.Substring(0,myid.Length-1); string sql="delete SD_AddAnnouce where id in ("+myid.ToString() + ")"; conn.Open(); SqlCommand comm=new SqlCommand(sql,conn); comm.ExecuteNonQuery(); Response.Write("<script>alert('删除公告成功!')</script>"); conn.Close(); dgbind(); } catch(Exception err) { Response.Write(err.ToString()); }
郁闷!怎么删除不了呢,它也不包错呀
哪個刪除不了?最後一頁的數據還是都是這樣?