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.
You can use the RowDeleting event, by storing the user name in the data key collection you can access it programmatically. Then, in the code behind use the data key to delete the record.
When the Delete Button is clicked, the OnRowDeleting event handler is executed. Inside the OnRowDeleting event handler, the Index of the GridView Row is determined and and it is used to delete the Row from the DataTable. Finally the DataTable is saved back to the ViewState and the GridView is again populated.
The GridView is populated using the records from the Customers table inside the Page Load event of the ASP.Net page. When the Edit Button is clicked, the GridView's OnRowEditing event handler is triggered. Here simply the EditIndex of the GridView is updated with the Row Index of the GridView Row to be edited.
Count; for (int i = 0; i <= iCount; i++) { GVGLCode. DeleteRow(i); } GVGLCode. DataBind(); c#
Here is a great article about typical usages of DataGrid.
Enjoy.
You can use the RowDeleting
event, by storing the user name in the data key collection you can access it programmatically.
<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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With