Article Options
Premium Sponsor
Premium Sponsor

 »  Home  »  Web Development  »  Using ASP.NET DataGrid Web Server Control
 »  Home  »  Web Development  »  ASP.NET  »  Using ASP.NET DataGrid Web Server Control
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...
Introduction

It would be hard to find a web developer who doesn’t need to use a DataGrid. This control is ideal for data presentation. Besides the visualization, it provides a powerful set of possibilities to edit, delete, add and update data. 

ASP.NET includes a well designed, powerful DataGrid Web Server Control which makes the life of web developers easier. I remember the time when we had to implement the grid's functionallity manually in classic ASP. And, although, a lot of time has passed since first ASP.NET was released and a lot of information exists on the  web, still there are a quite a bit of questions asked on forums about DataGrid features.

In this article I will demonstrate some DataGrid features such as editing, updating, deleting, sorting, paging, etc. which are quite often used by many web developers.    

Binding to data source

As usual, working with DataGrid begins with a data source. As data source, I used the well known Nwind database. Our DataGrid is bound on ‘Products’ table. Why Products table? Because it contains relationships with other tables such as ‘Categiries’, ‘OrderDetails’ and ‘Suppliers’.
Here is a screenshot of the tables relationship:


It models a  situation when  thedeveloper needs to display some related tables in DataGrid.

For binding, every table in the database should be converted to a DataTable object. How to do it? We need to create a new DataSet item, drag an object from Server Explorer. Visual Studio generates a DataSet class which contains a DataTable you entered.

Is that all? No. As we are going to do some operations on every table, it will be nice to create a small business layer. So, for every table, I created a component class which encapsulates logic for select, delete update a data from particular table.

DataGrid Layout

Let’s take a look at the HTML code in the ASPX page that creates the DataGrid:

<asp:datagrid id=ProductsDataGrid runat="server" 
  PageSize="<%# PageSize%>"
  AllowPaging="True"
  AllowSorting="True"
  BorderStyle="None"
  AutoGenerateColumns="False"
  DataSource="<%# Products %>"
  HorizontalAlign="Left"
  CellPadding=3
  Width="90%"
  BackColor="White"
  BorderColor="Black"
  PagerStyle-Mode="NextPrev" >

<ItemStyle Wrap="True" BorderStyle="None" CssClass="BodyText"></ItemStyle>
<HeaderStyle HorizontalAlign="Center" CssClass="ListHeader"></HeaderStyle>

As we see, DataGrid has powerful built-in features for customizing layout . You can play with such features by changing ItemStyle, HeaderStyle, color schema etc. After loading a page, the user is presented a DataGrid like this:

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