C# 如何在 GridView 中创建删除按钮?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1447180/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 17:37:04  来源:igfitidea点击:

How to create a delete button in GridView?

c#asp.netgridview

提问by Or Betzalel

I made another Column in my GridViewcalled delete. When delete is clicked, the row should be deleted or in other words, I need to get the current row's user name to delete it.

我在我的GridView删除中创建了另一个列。单击删除时,应删除该行,或者换句话说,我需要获取当前行的用户名才能将其删除。

  1. Which event should I use? (RowDeleting, Rowdeleted etc...)
  2. How do I get the username from the current row?
  1. 我应该使用哪个事件?(RowDeleting、Rowdeleted 等...)
  2. 如何从当前行获取用户名?

采纳答案by Eran Betzalel

Here is a great article about typical usages of DataGrid.

这是一篇关于DataGrid 典型用法的精彩文章。

Enjoy.

享受。

回答by lod3n

tablename.Rows.RemoveAt(datagrid1.currentcell.rowindex);

回答by Phaedrus

You can use the RowDeletingevent, by storing the user name in the data key collection you can access it programmatically.

您可以使用该RowDeleting事件,通过将用户名存储在数据键集合中,您可以以编程方式访问它。

<asp:GridView DataKeyNames="UserName" ID="GridView1" 
     runat="server" OnRowDeleting="GridView1_RowDeleting">

Then, in the code behind use the data key to delete the record.

然后,在后面的代码中使用数据键删除记录。

protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
  string userName = (string) GridView1.DataKeys[e.RowIndex].Value;
  DeleteUser(userName); 
}

回答by divya

1:-

1:-

protected void grdLst_RowDeleting(object sender, GridViewDeleteEventArgs e)

{

    int i = Convert.ToInt32(grdLst.DataKeys[e.RowIndex].Value);
    ob.DeleteCoverage(i);
    Response.Redirect("fullcoverage.aspx");
}

2:-

2:-

    GridViewRow row = (GridViewRow)grdlist.Rows[e.RowIndex];
    string name = row.Cells[1].Text;
    Response.Write(name.Trim());