GridView Control in ASP.NET: Part 3

Please you do read earlier parts of this article for complete understanding in GridView.

Part - 2

Using Fields with GridView Control

As to solve some of problems like enabling the GridView to render its columns automatically is that we give up any control over column formatting. For example, the BoxOfficeTotals column is displayed as a decimal amount without any currency formatting. The EnTRyDate column always displays in short-date and long-time format. The solution to such problems is to specify explicitly the fields that a GridView displays. The GridView control supports the following types of fields:

  • BoundField Enables us to display the value of a data item as text.
  • CheckBoxField Enables us to display the value of a data item as a check box.
  • CommandField Enables us to display links for editing, deleting, and selecting rows.
  • ButtonField Enables us to display the value of a data item as a button (image button, link button, or push button).
  • HyperLinkField Enables us to display the value of a data item as a link.
  • ImageField Enables us to display the valueof a data item as an image.
  • TemplateField Enables us to customize the appearance of a data item. 

Using BoundField

A BoundField always displays the value of a data item as text when a row is in normal display mode. When a row is selected for editing, a BoundField displays the value of a data item in a single line text field. The most important three properties of the BoundField class are the DataField, DataFormatString, and HeaderText properties.


<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""">

<script runat="server">


<html xmlns="">
<head runat="server">
    <form id="form1" runat="server">

        <asp:GridView ID="GridView1" 
        EmptyDataText="There are no data records to display." 
                HeaderText="Serial Number" 
                HeaderText="Name of Candidate" 
                SortExpression="NAME" />
                HeaderText="Address of Candidate" 
                SortExpression="ADDRESS" />
                SortExpression="MOBILE" />
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
            ConnectionString="<%$ ConnectionStrings:DatabaseConnectionString1 %>" 
            ProviderName="<%$ ConnectionStrings:DatabaseConnectionString1.ProviderName %>" 
            SelectCommand="SELECT [ID], [NAME], [ADDRESS], [MOBILE] FROM [MYTB]">


Notice that the GridView control includes an AutoGenerateColumns property that is assigned the value False. If we don't disable automatically generated columns, then both columns represented by the BoundFields and all the columns from the data source are displayed redundantly. BoundFields also supports several useful properties as listed below.

  • AccessibleHeaderText Enables we to add an HTML abbr attribute to the column header.
  • ApplyFormatInEditMode Enables us to apply the DataFormatString to the field when the row is in edit display mode.
  • ConvertEmptyStringToNull Enables we to convert an empty string "" into the value Nothing (null) when editing a column.
  • DataField Enables we to specify the name of the field that the BoundField displays.
  • DataFormatString Enables we to use a format string to format a data item.
  • FooterStyle Enables we to format the column footer.
  • FooterText Enables we to display text in the column footer.
  • HeaderImageUrl Enables we to display an image in the column header.
  • HeaderStyle Enables we to format the column header.
  • HeaderText Enables we to display text in the column header.
  • HtmlEncode Enables we to HTML-encode the value of a data item, which enables you to avoid script injection attacks.
  • InsertVisible Enables we to not display a column when inserting a new record (does not apply to the GridView control).
  • ItemStyle Enables we to format a data item.
  • NullDisplayText Enables we to specify textthat is displayed when a data item has the value Nothing (null).
  • ReadOnly Enables we to prevent the data item from being edited in edit mode.
  • ShowHeader Enables we to display the column header.
  • SortExpression Enables we to associate a sort expression with the column.
  • Visible Enables we to hide a column.

Note: Continue in Next Part.