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
How to create a delete button in GridView?
提问by Or Betzalel
I made another Column in my GridView
called 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
删除中创建了另一个列。单击删除时,应删除该行,或者换句话说,我需要获取当前行的用户名才能将其删除。
- Which event should I use? (RowDeleting, Rowdeleted etc...)
- How do I get the username from the current row?
- 我应该使用哪个事件?(RowDeleting、Rowdeleted 等...)
- 如何从当前行获取用户名?
采纳答案by Eran Betzalel
回答by lod3n
tablename.Rows.RemoveAt(datagrid1.currentcell.rowindex);
回答by Phaedrus
You can use the RowDeleting
event, 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());