Ths now is the most complex part of coding. From the user's side, everything is easy but from the developer side it is not.
When the user hits Update link, the DataGrid raises the UpdateCommand Event. The event handler receives an argument of type DataGridCommandEventArgs containing data related to this event. The most helpful data is DataItem that represents the selected item in the DataGrid control. As I found from many forums, the main question is how to retrieve a data which the user has input? The solution is to use code like the following:
Dim ProductIDLabel As Label = _ CType(e.Item.Cells(0).FindControl("ProductIDLabel"), Label) …
Dim ProductID As Integer = Int32.Parse(ProductIDLabel.Text) |
When we collected all changed values, we can update the currect row in DataTable.
Deleting rows from DataGrid
Sometimes the user would like to remove rows from a DataGrid. No problem. But in my view the user should be asked to confirm before deleting a row. For that, I added a client script which prompts the user to confirm a delete action.
<script language="javascript">
<!--
function OnDeleteClick(id)
{
if (window.confirm('Are you sure want to delete a product?'))
{
document.all("DeleteProduct").value = id;
__doPostBack("DeleteButton");
}
}
//-->
</script>
Here is the code for column where Delete button placed:
<asp:TemplateColumn>
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle"></ItemStyle>
<ItemTemplate>
<a href="javascript:OnDeleteClick('<%# DataBinder.Eval(Container, _
"DataItem.ProductID")%>');">Delete</a>
</ItemTemplate>
</asp:TemplateColumn>
The following is a screenshot of the prompt message:

Summary
In this article we have taken a walk through some features of the web DataGrid control such as sorting, paging, editing, updating, deleting, etc. We have also written some code snippets to demonstrate how to implement DataGrid features. The source code (both VB.NET and C#) is attached.