如何在按钮单击事件中从网格中删除所选数据?

我的页面上有一个网格,并使用复选框检查网格中的一行或多行,我想删除网格旁边的删除按钮,从网格中删除已检查的行。

在表中,我只有名称字段,没有像ID这样的字段。

我该如何删除记录。

提前致谢..

这是我的代码我在做什么: –

private void GetData() { ArrayList arr; if (ViewState["TotalRecords"] != null) { arr = (ArrayList)ViewState["TotalRecords"]; } else { arr = new ArrayList(); } for (int i = 0; i < grdlistWord.Rows.Count; i++) { CheckBox chk = (CheckBox)grdlistWord.Rows[i].Cells[1].FindControl("chkWord"); if (chk.Checked) { if (!arr.Contains(grdlistWord.Rows[i].Cells[1])) { arr.Add(grdlistWord.Rows[i].Cells[1]); } } else { if (arr.Contains(grdlistWord.Rows[i].Cells[1])) { arr.Remove(grdlistWord.Rows[i].Cells[1]); } } } ViewState["TotalRecords"] = arr; } protected void lnkbtnDelete_Click(object sender, EventArgs e) { try { int count = 0; ArrayList arr = (ArrayList)ViewState["TotalRecords"]; count = arr.Count; for (int i = 0; i < grdlistWord.Rows.Count; i++) { if (arr.Contains(grdlistWord.Rows[i].Cells[1].Text)) { Response.Write(grdlistWord.Rows[i].Cells[1].Text.ToString()); DeleteRecord(grdlistWord.Rows[i].Cells[1].Text.ToString()); arr.Remove(grdlistWord.Rows[i].Cells[1].Text); } } ViewState["TotalRecords"] = arr; GridBind(); } catch (SqlException ex) { ex.ToString(); } } private void DeleteRecord(string word) { string query = "delete from searchword where word=@word"; SqlCommand cmd = new SqlCommand(query, con); cmd.Parameters.AddWithValue("@word", word); con.Open(); cmd.ExecuteNonQuery(); con.Close(); } 

gridview html详细信息:

  
List Records not exist!

  protected void lnkbtnDelete_Click(object sender, EventArgs e) { int counter = 0; List words = new List(); foreach (GridViewRow rowitem in grdlistWord.Rows) { if (((CheckBox)rowitem.Cells[0].FindControl("chkWord")).Checked == true)//i consider that the check box is in the first column index ---> 0 { counter++; words.Add(rowitem.Cells[1].Text); //i consider that the word is in the second column index ---> 1 } } ///////////////////////////////////////////////////////////// if(counter == 0) //no checks { //show some message box to clarify that no row has been selected. } ///////////////////////////////////////////////////////////// if (counter == 1) //one check { DeleteRecord(words[0]); //Show some message box to clarify that the operation has been executed successfully. } ///////////////////////////////////////////////////////////// if (counter > 1) //more than one check { for(int i=0; i 

为此,您首先在表中设置主键ID,然后将其作为Grid的源提取。

绑定网格后,您应该将该ID保存在隐藏字段中。 看下面的代码:

         

然后在删除按钮单击事件获取选定的行ID:

  protected void Btn_Click(object sender, EventArgs e) { int[] OrderIDList = new int[gvwID.Rows.Count]; int index = 0; for (int count = 0; count < gvwID.Rows.Count; count++) { if (gvwID.Rows[count].FindControl("chkSelect") != null) { if (((CheckBox)gvwID.Rows[count].FindControl("chkSelect")).Checked) { if (gvwID.Rows[count].FindControl("HiddenField1") != null) { string OrderID = ((HiddenField)gvwID.Rows[count].FindControl("HiddenField1")).Value; OrderIDList[index++] = Convret.ToInt32(OrderID); } } } } 

然后从OrderIDList创建一个附加字符串并将其传递给存储过程。 从存储过程使用附加的字符串创建一个xml。 循环遍历xml并获取每个id并执行删除。

请参阅以下程序:

  @IDList varchar(MAX) DECLARE @xmlUserIDs xml SELECT @xmlUserIDs = CONVERT(xml,'' + REPLACE(@IDList,',','') + '')// creates xml from appended string DELETE FROM [TableName] WHERE [TableID] IN (SELECT [Value] = TCvalue('.','int') FROM @xmlUserIDs.nodes('/root/cat') T(C)); 

希望这有助于你..