DataGridColumn.ItemStyle Property

Gets the style properties for the item cells of the column.

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

public virtual TableItemStyle ItemStyle { get; }

Property Value

Type: System.Web.UI.WebControls.TableItemStyle
A TableItemStyle that contains the style properties for the item cells of the column. The default value is an empty TableItemStyle.

Use this property to provide a custom style for the item cells of the column. Common style attributes that can be adjusted include foreground color, background color, font, and content alignment within the cell. Providing a different style enhances the appearance of the column in the DataGrid control.

The following code example demonstrates how to use the ItemStyle property to get the style properties of the item cells in a 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)));

         // 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);


         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) 
            ItemsGrid.DataSource = CreateDataSource();


      void Button_Click(Object sender, EventArgs e) 

         // Count the number of selected items in the DataGrid control.
         int count = 0;

         // Display the selected times.
         Message.Text = "You Selected: <br />";

         // Iterate through each item (row) in the DataGrid control and
         // determine whether it is selected.
         foreach (DataGridItem item in ItemsGrid.Items)

            DetermineSelection(item, ref count);        


         // If no items are selected, display the appropriate message.
         if (count == 0)

            Message.Text = "No items selected";



      void DetermineSelection(DataGridItem item, ref int count)

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

         // If the item is selected, display the appropriate message and
         // increment the count of selected items.
         if (selection != null)

           if (selection.Checked)
              Message.Text += "- " + item.Cells[1].Text + "<br />";



      void Selection_Change(Object sender, EventArgs e)

         // Set the background color for the items in the first column
         // of the DataGrid control.
         ItemsGrid.Columns[0].ItemStyle.BackColor = 



<head runat="server">
    <title>DataGridColumn ItemStyle Example</title>

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

      <h3>DataGridColumn ItemStyle Example</h3>

      Select a background color for the items in the first column.

      <br /><br />

      <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"





      <br /><br />

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

      <br /><br />

      <asp:Label id="Message"

      <hr />

      BackColor: <br /> 

      <asp:DropDownList id="List"

         <asp:ListItem Selected="True" Value="White"> White </asp:ListItem>
         <asp:ListItem Value="Silver"> Silver </asp:ListItem>
         <asp:ListItem Value="DarkGray"> Dark Gray </asp:ListItem>
         <asp:ListItem Value="Khaki"> Khaki </asp:ListItem>
         <asp:ListItem Value="DarkKhaki"> Dark Khaki </asp:ListItem>




.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
© 2014 Microsoft