LogoASPAlliance: Articles, reviews, and samples for .NET Developers
WebForms in a DataGrid
by . .
Average Rating: 
Views (Total / Last 10 Days): 20975/ 54

The Setup DataGrid

Why use web forms?

The most obvious place for using web forms in a DataGrid is for editing. It can give you stricter control over the data that you can input, but you are not just limited to editing. I'll show you throughout this article where you can put them in for normal viewing.

Before we begin

For all of these examples we'll be using the following DataGrid -

<asp:DataGrid id="dg" runat="server"

Live Demo

Note - To see the code used to create the datasource for all this, click here.

Note - You may need to add some event handlers later on.

CheckBoxes and RadioButtons

The TemplateColumn

Recently, I have been trying to get more and more out of a DataGrid and have found the usual ways to display columns (boundcolumns, hyperlinkcolumns etc) don't have enough. The TemplateColumn has a lot more, it allows you to add much more to your DataGrid. Take the following two examples -

CheckBox / CheckBoxList

A checkbox can be used in a column to show if something is true, yes, available, checked etc. or not. It sometimes looks better than having a 'Yes' or 'No'. You can also use it as a delete checkbox, where a user checks the items that he/she wants deleted and then clicks an update button in the footer.

<asp:checkbox runat="server" Checked='<%# Container.DataItem("Married") %>' Enabled="False" id="chk1" />

Live Demo

This will check to see if the person is Married (defined by True or False in the DataSet) and check the box or not. When editing, you can just switch the Enabled to True.

RadioButton / RadioButtonList

I would use these in an edit template. For example -

<asp:radiobuttonList runat="server">
<asp:listitem value="Trek">Star Trek</asp:listitem>
<asp:Listitem value="Wars">Star Wars</asp:listitem>

Live Demo

These are just samples of what you can do in TemplateColumns.

When to use TemplateColumns

All of the examples here use template columns to display the data. But remember that there are other kinds of columns, but sadly, they all seem kind of pitiful (when trying to do more than just display and edit data). eg. The Hyperlink column will not allow more than one column of text to be displayed, you have to use a template column -

<a href=../view.aspx?id=<%# Container.DataItem("ID") %>><%# Container.DataItem("ID") & Container.DataItem("Name") %></a> </itemtemplate>
Validation in a DataGrid

Validating in a DataGrid

Validating the data before it is even submitted saves a round-trip. However, for some database logic you may need to have a round-trip (for CustomValidators).

<%# Container.DataItem("Age") %>
<asp:RegularExpressionValidator runat="server" ControlToValidate="txtAge" ErrorMessage="Invalid input for Age"
ValidationExpression="[0-9]{2}" /><br>
<asp:textbox runat="server" id="txtAge" value='<%# Container.DataItem("Age") %>' />

Live Demo

This checks to make sure that it is a two digit number.


This article has more been about using TemplateColumns than using web forms, however we have used a series of server controls (RadioButton, checkbox and validation) in a DataGrid to give it more functionality. In the related article - "Using Dropdowns in DataGrids" I will explore using a DropDownList which is more complicated than the usual server controls.

Product Spotlight
Product Spotlight 

©Copyright 1998-2021  |  Page Processed at 2021-12-02 7:47:27 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search