Santry Technology Solutions, Content Management, DotNetNuke, SharePoint Consulting
Register | Login
Tuesday, December 02, 2008

Sections
  
About Us
  
Partners
Downloads
  
 WWWCoder.com Resource Directory

Introducing the GridView Control
9/26/2004 6:08:47 PM

In this article we will bind a GirdView control to an AccessDataSource control, and cover some of the new features that comes with the GridView control.

In this article we'll introduce you to the successor to the datagrid that comes with ASP.NET 1.1. This control is called the GridView control. The GridView control provides more flexibility in displaying and working with data from your database, combined with the DetailsView, and FormView controls ASP.NET 2.0 should get your applications running in very little time.

The GridView control enables you to connect to a datasource and display data bound to the control. We first want to configure an AccessDatasource control in this example and then bind this control to our DataView control.

DataSource Controls

In order to add a control you can go to design mode within Visual Web Developer and drag and drop a datasource control. There are several datasource controls you can select from:

  • SQLDataSource
  • AccessDataSource
  • ObjectDataSource
  • XMLDataSource
  • SiteMapDataSource

In this example we will select an AccessDataSource control to place in our page. Once you drop the control into your page you can modify a couple attributes, let's look at the source of the control:

<asp:accessdatasource id="NorthwindProducts" runat="server"
    selectcommand="SELECT * FROM [Customers]"
    datafile="~/Data/Northwind.mdb">
</asp:accessdatasource>

You can see in our example above, we provided an ID for our control called NorthwindProducts, enabled the control to run at the server, created a query for the control, and pointed the control to our Access database which is located in the Data folder off our application root.

GridView Control

Now that we have a data source control in the page, let's bind that control to a GridView control. Again, this can be done in the IDE by dragging and dropping a GridView, and then binding the control to our AccessDataSource control we added to the page previously.

Once you add the control to the page in design mode, you'll notice a small arrow on the right upper corner of the GridView. Click on this control to view the attributes of the control. This brings up a dialog where you can specify which datasource to bind to if there is one existing in the page.

You'll also notice some great new features to the control. ASP.NET 2.0 enables you to do paging, sorting, and selection, just by specifying a couple options in the control. Now, it is much easier to specify and control the various attributes of the control. In the past, paging and sorting would have taken a lot of custom code to write, now in ASP.NET 2.0 it is provided for you.

In our example, we'll just bind the control to our AccessDataSource control. Now let's look at the resulting code:

<asp:gridview id="grdNorthwindProducts" runat="server"
datasourceid="NorthwindProducts" DataKeyNames="CustomerID" AutoGenerateColumns="False">
<Columns>
<asp:BoundField ReadOnly="True" HeaderText="CustomerID" DataField="CustomerID" SortExpression="CustomerID"></asp:BoundField>
<asp:BoundField HeaderText="CompanyName" DataField="CompanyName" SortExpression="CompanyName"></asp:BoundField>
<asp:BoundField HeaderText="ContactName" DataField="ContactName" SortExpression="ContactName"></asp:BoundField>
<asp:BoundField HeaderText="ContactTitle" DataField="ContactTitle" SortExpression="ContactTitle"></asp:BoundField>
<asp:BoundField HeaderText="Address" DataField="Address" SortExpression="Address"></asp:BoundField>
<asp:BoundField HeaderText="City" DataField="City" SortExpression="City"></asp:BoundField>
<asp:BoundField HeaderText="Region" DataField="Region" SortExpression="Region"></asp:BoundField>
<asp:BoundField HeaderText="PostalCode" DataField="PostalCode" SortExpression="PostalCode"></asp:BoundField>
<asp:BoundField HeaderText="Country" DataField="Country" SortExpression="Country"></asp:BoundField>
<asp:BoundField HeaderText="Phone" DataField="Phone" SortExpression="Phone"></asp:BoundField>
<asp:BoundField HeaderText="Fax" DataField="Fax" SortExpression="Fax"></asp:BoundField>
</Columns>
</asp:gridview>

You can see all how the IDE added the code needed to bind and display our data. Now let's save the page and view the results in the Web browser.

There you have it, instant data in a Web page. In later articles we'll cover working with the GridView in more detail.


Page Options:
format for printing  Format for Printer
email article  Email Page
add to your favorites   Add to Favorites
How would you rate the quality of this content?
Poor - - Excellent
Comments?
Overall Rating:
Comments Left:
Left on 11/29/2008 12:40:47 AM by Anonymous
Comments: Not enough inf
Left on 10/21/2008 5:07:50 AM by Anonymous
Comments: Left on 5/1/2008 9:48:39 PM by Anonymous
Comments: advantages andd rawbacks of gridview control
No ratings available.
No ratings available.
Left on 10/14/2008 4:47:54 AM by Anonymous
Comments: poor article
Left on 9/19/2008 7:18:24 AM by Anonymous
Comments: fudu likha hai
No ratings available.
Left on 9/7/2008 4:19:57 AM by Anonymous
Comments: good tips for me...
Left on 6/9/2008 12:50:14 AM by Anonymous
Comments: very poor.ican't see this type ..
Left on 5/1/2008 9:48:39 PM by Anonymous
Comments: advantages andd rawbacks of gridview control
No ratings available.
Left on 1/24/2008 6:47:34 AM by Anonymous
Comments: fdaefwefw
Left on 1/11/2008 6:11:02 AM by Anonymous
Comments: poor,very poor as per my view
Left on 11/16/2007 4:31:00 AM by Anonymous
Comments: Waste
Left on 7/19/2007 5:34:14 AM by Anonymous
Comments: stupid
Left on 7/16/2007 7:09:39 AM by Anonymous
Comments: ll;ll;
Left on 7/3/2007 1:16:51 PM by Anonymous
Comments: dsf dsf
No ratings available.
Left on 3/21/2007 11:00:06 AM by Anonymous
Comments: fsd
fsd
No ratings available.
Left on 2/8/2007 4:17:05 AM by Anonymous
Comments: bettere an example using hand made code
Left on 12/19/2006 4:41:44 AM by Anonymous
Comments:
Left on 9/13/2006 7:28:07 AM by Anonymous
Comments: Mastering help for the begineer like me!!
Left on 8/23/2006 11:33:37 AM by Anonymous
Comments: how to read data from grid view
Left on 8/9/2006 2:03:58 AM by Anonymous
Comments: Good, and you will include insert, delete and update opertion performed in side gridview then it is efficient to referring the gridview

No ratings available.
Left on 4/28/2006 2:16:29 AM by Anonymous
Comments: Really Good Description About GridView

No ratings available.
Left on 3/30/2006 3:01:06 PM by Anonymous
Comments: Comments from the following blog: Kent Sharkey, located at: http://www.acmebinary.com/blogs/kent/archive/2006/03/30/516.aspx
No ratings available.
Left on 3/24/2006 1:45:04 PM by Anonymous
Comments: great quick primer, thanks much.
Left on 3/14/2006 6:29:44 AM by Anonymous
Comments: good.But describe more.
Left on 2/26/2006 4:23:34 PM by Anonymous
Comments: Not terribly helpful

Left on 2/18/2006 8:29:44 AM by Anonymous
Comments: How do you extract data from a particular cell? Suppose I have the primary key in cell 1, but in the GridView, I ade it invisible, but I want to know what is the value.

Thanks,
Left on 1/25/2006 2:17:21 PM by Anonymous
Comments: i would like to see one of the fields made a template field using a method to change some of the data being displayed
No ratings available.
Left on 12/14/2004 10:27:05 AM by Anonymous
Comments: good intro to GridView control
No ratings available.
Left on 11/30/2004 3:08:35 PM by Anonymous
Comments: can you post an example of doing same but
Left on 11/30/2004 3:03:33 PM by Anonymous
Comments: can you post an example of doing same but runtime? to be more spec to define source for gridview in runtime including connections and so on?
No ratings available.
Left on 11/17/2004 12:42:39 AM by Anonymous
Comments: blows
Left on 11/15/2004 11:20:32 PM by Anonymous
Comments: does a good job explaining gridview, but i was expecting more.  where's the next article on detailview?
No ratings available.
Left on 11/13/2004 12:33:27 AM by Anonymous
Comments: If anyone place a girdview in a webform this properties will appear automatically
Left on 11/12/2004 9:48:28 AM by Anonymous
Comments: This is a great look at the next gen, but it really doesn't go far enough... show us the goods!
No ratings available.
Left on 11/12/2004 12:59:49 AM by Anonymous
Comments: Kult!
Left on 11/10/2004 10:16:54 AM by Anonymous
Comments: No, the GridView is not a "light" version of the data grid, it is the next version of the datagrid.
No ratings available.
Left on 11/9/2004 11:49:10 AM by Anonymous
Comments: GridView is a "light" version of DataGrid.
Left on 11/9/2004 10:39:21 AM by Anonymous
Comments: The difference between a GridView and DataGrid are.....???
Left on 11/9/2004 8:16:06 AM by Anonymous
Comments: This is great, but can it finally display results vertically as opposed to horizontally?
  

 Latest Articles
  

 Latest News
  

 

Spotlight
Syndication

 


 


Digg This
 


DotNetNuke Platinum Benefactor

  
 

 Terms Of Use | Privacy Statement
 Copyright 2008 - Santry Technology Solutions, Box 172, Girard, PA 16417, (814) 774-0970