Tuesday, March 22, 2005

[itsdifferent] Common Datagrid Mistakes

---------- Forwarded message ----------
From: Deven Goratela
Date: Fri, 18 Mar 2005 14:53:47 +0530
Subject: [itsdifferent] Common Datagrid Mistakes
To: itsdifferent@yahoogroups.com

The Datagrid control in Microsoft(r) ASP.NET is one of the most powerful
and useful web controls that has been provided by the ASP.NET
powers-that-be. Although this control is easy to use, the Datagrid can
be easy to become frustrated with. The following mistakes are ones
that many, many people make, from beginners to experienced .NET pros.
You can spot frustrated souls asking questions about these very issues
in the ASP.NET newsgroups and forums. Following the relatively simple
steps outlined in this article can help you avoid these mistakes and
save yourself quite a bit of development time.
Creating Tabular Data by Not Using the Datagrid When You Could Be

I know you're out there, the old-fashioned people in the ASP.NET world
who still have code like this:Response.Write("

")While
MyDataReader.Read()Response.Write("")Response.Write("")Response.Write("")LoopResponse.Write("
")Response.Write(MyDataReader(0))Response.Write("
")

The above code could be simplified to merely:

along with a
call to the .DataBind() method. Even if you need specialized control
over the HTML output, use one of the data web controls when your user
interface repeats based on a set of records.
Forgetting to Check for IsPostBack in the Page_Load Event

One of the most common mistakes around is forgetting to check the
page's IsPostBack condition before databinding. For example, when the
Datagrid is in Edit mode, omitting this check will cause the edited
values to be overwritten with the original values from the data
source. There is, however, at least one major exception to this rule,
see Enduring an Over-sized ViewState.

The following is a typical Page_Load event which includes the
IsPostBack checking. BindGrid() is a routine that populates and sets
the Datagrid's data source, and calls the DataBind() method.Sub
Page_Load If Not IsPostBack Then BindGrid() End IfEnd Sub
Sticking with Auto-Generated Columns When Your Circumstances Require
More Flexibility

If your Datagrid situation requires any kind of special formatting or
the use of any other web controls inside your Datagrid, then you need
to turn off AutoGenerateColumns. Leaving this property set to True
(the default) is only useful in the simplest of Datagrid scenarios,
but for nearly any real-world application, you need to set
AutoGenerateColumns to False, and specify the columns explicitly in
the section of the Datagrid declaration. Microsoft
Visual Studio(r) .NET users can use the Property Builder to create these
columns graphically.

Note If you leave AutoGenerateColumns set to true and specify the
columns in your Datagrid's section, you will end up with a
duplicate set of columns. The specifically declared columns will
appear first, followed by all of the auto-generated columns.
Trying to Refer to a Control Inside a Datagrid Item by its ID Alone

Many people don't realize that if you have a control inside a the
ItemTemplate of a TemplateColumn in your Datagrid—let's say it's a
TextBox control with an ID of "MyTextBox"—that you can't refer to that
control directly in your codebehind or