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:4.66666666666667 out of 5
 15 people have rated this page
Article Score33398
Related Articles
Attachments
Comments    Submit Comment

Comment #1  (Posted by an unknown user on 12/11/2005)
Rating
realy usefull,
 
Comment #2  (Posted by Johana on 12/28/2005)
Rating
El articulo es interante pero muy engorroso, mi pregunta es como podria, hacer que el segundo combo carge con una funcion que reciba 2 valores, el primero q sea resultado del codigo del primer combo y el segundo valor de un campo del grid, eso es lo q me gustaria hacer pero sin tantas clases no se si puedas resolver mi duda.
Gracias
 
Comment #3  (Posted by Misha Zhutov on 12/29/2005)
Rating
My apologies - I can't read Spanish
 
Comment #4  (Posted by an unknown user on 01/06/2006)
Rating
Very useful and demostrates very common operations that are used when programming with the datagrid.
 
Comment #5  (Posted by an unknown user on 01/06/2006)
Rating
Very useful and demostrates very common operations that are used when programming with the datagrid.
 
Comment #6  (Posted by David Wilburn on 01/08/2006)
Rating
Very good article! For a person such as myself that has very little ASP.Net experience, it was very informative.

Much appreciated.
 
Comment #7  (Posted by an unknown user on 03/14/2006)
Rating
hi,
this is very good article.
 
Comment #8  (Posted by AFSUR HUSSAIN on 03/25/2006)
Rating
ya really its fine friends..,


 
Comment #9  (Posted by an unknown user on 04/18/2006)
Rating
good
 
Comment #10  (Posted by an unknown user on 05/19/2006)
Rating
I want to know data update in gtagrid.I can lern lot of thing in your article. Thanks lot for your help.
 
Comment #11  (Posted by an unknown user on 08/10/2006)
Rating
Great but:"The source code (both VB.NET and C#) is attached."...i cannot see it
 
Comment #12  (Posted by Misha Zhutov on 08/11/2006)
Rating
The attachment is on the last article's page, here is direct link on it: http://www.devcity.net/Download.aspx?FileType=Attachment&FileName=WebDataGridExamples.zip&File=3532cd2b-353d-4277-a05e-e6f20d4a268c.zip
 
Sponsored Links