Using GridView controls to display data in ASP.NET
Part two of an eight-part series of blogs

The gridview control is at the heart of every ASP.NET webpage, displaying rows of data and allowing you to add, edit and delete records. This lengthy blog explains how to master this complex control.

  1. An introduction to GridView Controls
  2. Traps to Avoid when using Gridviews (this blog)
  3. Creating GridView Controls
  4. Formatting Gridviews
  5. Template Fields / Template Columns
  6. Conditional formatting (RowDataBound Events)
  7. You clicked on a Gridview? The RowCommand Event
  8. Sorting a Gridview

This blog is part of a larger online ASP.NET online tutorial.  Wise Owl's main business is running classroom-based training courses - have a look at our ASP.NET or other .NET courses.

Posted by Andy Brown on 24 September 2012

You need a minimum screen resolution of about 700 pixels width to see our blogs. This is because they contain diagrams and tables which would not be viewable easily on a mobile phone or small laptop. Please use a larger tablet, notebook or desktop computer, or change your screen resolution settings.

Traps to Avoid when using Gridviews

There are a few red herrings to avoid when displaying data on a webpage.

 Avoid Alternative Data Controls

There are a fair few competing data controls in ASP.NET:

The ASP.NET data controls

The data controls in ASP.NET include Repeater, DataList and ListView controls as well as GridView controls.

 

However, there are good reasons to stick to using GridView controls initially:

Alternative Why not to use it
Repeater A Repeater is a very simple control which provides the header, footer and rows for an HTML table, and uses data from a database to fill in the cells.  However, it seems pointless learning about repeater controls when a gridview does everything a repeater does and then so much more.  Learning to use a repeater would be a bit like learning sign language before learning to speak.
DataList A DataList allows you to display data in a wrapping format (so if you're displaying shopping items, for example, you could display 3 items on each row).  However, a ListView is a more powerful control, which effectively renders the DataList obsolete.
ListView ListView controls are new in ASP.NET 3.5, and allow you to have complete control over the HTML generated for a display of data.  They're pretty powerful and well worth learning - once you've mastered the GridView.  The good news is that the way the two controls work is similar.

Avoid Skins

You can create server-side skins in a gridview to control how they appear on screen:

Setting the skin for a gridview

Here the SkinId is set to gvJazzy.

 

However, gridviews are easier to control if you use client-side CSS instead (as in the above example, which also sets the CSS class to gv).  You can see more discussion on the relative merits of skins and styles earlier in this blog series.

Avoid data sources

The first thing Visual Studio invites you to do when you've added a gridview is to assign a data source to it:

Assigning data source to gridview

You can create a new data source or use an existing one.

However, if you take this approach then - sooner or later - you'll reach a point when you can't see how to adapt what you've been given.  That's why we'll be using data classes instead.

 

So now we've seen what not to do, let's add a gridview!

This blog has 0 threads Add post