DataGridColumn.HeaderText Property


Gets or sets the text displayed in the header section of the column.

Namespace:   System.Web.UI.WebControls
Assembly:  System.Web (in System.Web.dll)

public virtual string HeaderText { get; set; }

Property Value

Type: System.String

The text displayed in the header section of the column. The default value is String.Empty.

Use the HeaderText property to specify or determine the text displayed in the header section of the column. If you want to display an image instead of text in the header section of the column, set the HeaderImageUrl property instead of this property.


Text is not HTML-encoded before it is displayed in the header section of the column. This makes it possible to embed script within HTML tags in the text. If this property is set dynamically using user input, be sure to validate the value to reduce security vulnerabilities.

The following code example demonstrates how to use the HeaderText property to specify the text for the header section of the column.

<%@ Page Language="C#" AutoEventWireup="True" %>
<%@ Import Namespace="System.Data" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<html xmlns="" >
   <script runat="server">

      ICollection CreateDataSource() 

         // Create sample data for the DataGrid control.
         DataTable dt = new DataTable();
         DataRow dr;

         // Define the columns of the table.
         dt.Columns.Add(new DataColumn("IntegerValue", typeof(Int32)));
         dt.Columns.Add(new DataColumn("StringValue", typeof(string)));
         dt.Columns.Add(new DataColumn("CurrencyValue", typeof(double)));
         dt.Columns.Add(new DataColumn("BooleanValue", typeof(bool)));

         // Populate the table with sample values.
         for (int i = 0; i < 5; i++) 
            dr = dt.NewRow();

            dr[0] = i;
            dr[1] = "Item " + i.ToString();
            dr[2] = 1.23 * (i + 1);
            dr[3] = false;


         // To persist the data source between posts to the server, store
         // it in session state.  
         Session["Source"] = dt;

         DataView dv = new DataView(dt);
         return dv;


      void Page_Load(Object sender, EventArgs e) 

         // Load sample data only once, when the page is first loaded.
         if (!IsPostBack) 
            // Make sure to set the header text before binding the data to 
            // the DataGrid control; otherwise, the change will not appear 
            // until the next time the page is refreshed.
            ItemsGrid.Columns[0].HeaderText = "Item";

            ItemsGrid.DataSource = CreateDataSource();


      void Button_Click(Object sender, EventArgs e) 

         double subtotal = 0.0;

         // Update the data source with the user's selection and 
         // calculate the subtotal.
         DataTable dt = UpdateSource(ref subtotal);

         // Display the subtotal in the footer section of the third column.
         ItemsGrid.Columns[2].FooterText = 
             "Subtotal: " + subtotal.ToString("c");

         // Create a DataView and bind it to the DataGrid control.
         DataView dv = new DataView(dt);
         ItemsGrid.DataSource = dv;


      DataTable UpdateSource(ref double subtotal)

         // Retrieve the data table from session state.
         DataTable dt = (DataTable)Session["Source"];

         // Iterate through the Items collection and update the data source
         // with the user's selections. If an item is selected, add the
         // amount of the item to the subtotal.
         foreach (DataGridItem item in ItemsGrid.Items)

            // Retrieve the SelectCheckBox CheckBox control from the 
            // specified item (row) in the DataGrid control.
            CheckBox selection = (CheckBox)item.FindControl("SelectCheckBox");

            if (selection != null)

               // Update the BooleanValue field with the value of the check box.
               dt.Rows[item.ItemIndex][3] = selection.Checked;

               // Add the value of the item to the subtotal if the item is 
               // selected.
               if (selection.Checked)
                  subtotal += 



         // Save the data source.
         Session["Source"] = dt;

         return dt;



<head runat="server">
    <title>DataGridColumn HeaderText and FooterText Example</title>

   <form id="form1" runat="server">

      <h3>DataGridColumn HeaderText and FooterText Example</h3>

      <b>Product List</b>

      <asp:DataGrid id="ItemsGrid"

         <HeaderStyle BackColor="#00aaaa">

         <FooterStyle BackColor="#00aaaa">


            <asp:BoundColumn DataField="IntegerValue"/>

            <asp:BoundColumn DataField="StringValue" 

            <asp:BoundColumn DataField="CurrencyValue" 

               <ItemStyle HorizontalAlign="Right">


            <asp:TemplateColumn HeaderText="Select Item">


                  <asp:CheckBox id="SelectCheckBox"
                       Text="Add to Cart"
                       Checked='<%# DataBinder.Eval(Container.DataItem, "BooleanValue") %>'





      <br /><br />

      <asp:Button id="SubmitButton"
           OnClick = "Button_Click"



.NET Framework
Available since 1.1
Return to top