注册 登录
编程论坛 ASP.NET技术论坛

我在datagrid下面放了一个删除按纽,当我选中哪个我就让它删除哪个

lmyh5201 发布于 2007-06-12 11:14, 1125 次点击
我在datagrid下面放了一个删除按纽,当我选中哪个我就让它删除哪个,怎么实现呀
11 回复
#2
川流不息2007-06-12 12:00

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();//操作結束後重新顯示數據,保持數據的更新
}

#3
lmyh52012007-06-12 12:35
我这样写为什么只删除一个呢
private void Button2_Click(object sender, System.EventArgs e)
{
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=this.DataGrid1.DataKeys[i].ToString();
string sql="delete SD_AddAnnouce where id="+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());
}
}
#4
lmyh52012007-06-12 13:12

好了,我改好了!这样就行了!
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());
}

#5
川流不息2007-06-12 13:36
你把頁面點到最後一頁(就是DataGrid),再全部刪除最後一頁的數據看看,會不會報錯。
#6
lmyh52012007-06-12 13:53

郁闷!怎么删除不了呢,它也不包错呀

#7
川流不息2007-06-12 14:15

哪個刪除不了?最後一頁的數據還是都是這樣?

#8
lmyh52012007-06-12 14:17
就是我把最后一页的数据全选了之后,进行删除,就删除不了,而且还没有包错
#9
川流不息2007-06-12 14:28
你把SQL語句打出來,看看,這條語句執行的話有沒有結果。
#10
lmyh52012007-06-12 14:33
打印出来是这个delete SD_AddAnnouce where id in (449,450,451,452,453,454)
#11
川流不息2007-06-12 14:42
把它拿到查詢分析器看看能不能執行成功。
#12
lmyh52012007-06-12 14:51
好了,能删除了,原来我把数据库的表明写错了!呵呵!谢谢斑竹!!!
1