BLOGS BY TOPIC
BLOGS BY AUTHOR
BLOGS BY YEAR
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.
- An introduction to GridView Controls
- Traps to Avoid when using Gridviews (this blog)
- Creating GridView Controls
- Formatting Gridviews
- Template Fields / Template Columns
- Conditional formatting (RowDataBound Events)
- You clicked on a Gridview? The RowCommand Event
- Sorting a Gridview
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 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.|
You can create server-side skins in a gridview to control how they appear on screen:
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:
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!