Article Options
Premium Sponsor
Premium Sponsor

 »  Home  »  Web Development  »  Using ASP.NET DataGrid Web Server Control  »  DataGrid updating
 »  Home  »  Web Development  »  ASP.NET  »  Using ASP.NET DataGrid Web Server Control  »  DataGrid updating
Using ASP.NET DataGrid Web Server Control
by Misha Zhutov | Published  11/03/2005 | Web Development ASP.NET | Rating:
Misha Zhutov

Misha Zhutov works as a Software Architect for SubMain(), a division of vbCity.com, LLC.

During more than 7 years of his programmer career Misha has developed various applications including desktop, client-server and Internet development. He has been working with Microsoft technologies since 1997 and has been developing with Microsoft .NET since the Beta 2 release in 2001.

Misha has a Bachelors and a Masters degree in Radio-Electronic Engineering from Kharkov Aviation University.

His primary areas of expertise are VB.NET, C#, ASP and Visual Basic.

 

View all articles by Misha Zhutov...
DataGrid updating

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)
Generated using PrettyCode.Encoder

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.

How would you rate the quality of this article?
1 2 3 4 5
Poor Excellent
Tell us why you rated this way (optional):

Article Rating
The average rating is: No-one else has rated this article yet.

Article rating:3.06358381502887 out of 5
 173 people have rated this page
Article Score57825
Sponsored Links