This documentation is archived and is not being maintained.

DataGridItem.DataItem Property

Gets or sets the data item represented by the DataGridItem object in the DataGrid control.

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

public virtual Object DataItem { get; set; }
/** @property */
public Object get_DataItem ()

/** @property */
public void set_DataItem (Object value)

public function get DataItem () : Object

public function set DataItem (value : Object)

Property Value

A System.Object that represents a data item in the DataGrid control.

Use the DataItem property to specify or determine the properties of a data item represented by the DataGridItem object in the DataGrid control.

NoteNote

This property applies only to data items in the DataGrid control. The ItemType property must be set to ListItemType.Item, ListItemType.AlternatingItem, ListItemType.SelectedItem, or ListItemType.EditItem.

The following code example demonstrates how to use the DataItem property to display the contents of the data items in the DataGrid control.

NoteNote

The following code example uses the single-file code model and might not work correctly if copied directly into a code-behind file. This code example must be copied into an empty text file that has an .aspx extension.

<%@ Page Language="C#" AutoEventWireup="True" %>
<%@ Import Namespace="System.Data" %>
 
<html>
<script language="C#" runat="server">
 
   DataTable Cart;
   DataView CartView;
 
   ICollection CreateDataSource() 
   {
      DataTable dt = new DataTable();
      DataRow dr;
 
      dt.Columns.Add(new DataColumn("IntegerValue", typeof(Int32)));
      dt.Columns.Add(new DataColumn("StringValue", typeof(string)));
      dt.Columns.Add(new DataColumn("CurrencyValue", typeof(double)));
 
      for (int i = 0; i < 10; i++) 
      {
         dr = dt.NewRow();
 
         dr[0] = i;
         dr[1] = "Item " + i.ToString();
         dr[2] = 1.23 * (i+1);
 
         dt.Rows.Add(dr);
      }
 
      DataView dv = new DataView(dt);
      return dv;
   }
 
   void Page_Load(Object sender, EventArgs e) 
   {
 
      if (!IsPostBack) 
      {
         // Need to load this data only once.
         ItemsGrid.DataSource = CreateDataSource();
         ItemsGrid.DataBind();
      }
 
   }
 
   void Item_Created(Object sender, DataGridItemEventArgs e) 
   {
 
      if (e.Item.ItemType == ListItemType.Item ||
          e.Item.ItemType == ListItemType.AlternatingItem ||
          e.Item.ItemType == ListItemType.SelectedItem ||
          e.Item.ItemType == ListItemType.EditItem)

      {   
         Label1.Text += "<br>" + e.Item.ItemType.ToString() +
                        " - " + ((DataRowView)e.Item.DataItem).Row.ItemArray[1].ToString();
      }

      else

      {
         Label1.Text += "<br>" + e.Item.ItemType.ToString();
      }
 
   }
 
</script>
 
<body>
 
   <form runat=server>

      <h3>DataGridItem DataItem Example</h3>
 
      <asp:DataGrid id="ItemsGrid" runat="server"
           BorderColor="black"
           BorderWidth="1"
           CellPadding="3"
           ShowFooter="true"
           OnItemCreated="Item_Created"
           AutoGenerateColumns="true">

         <HeaderStyle BackColor="#00aaaa">
         </HeaderStyle>

         <FooterStyle BackColor="#00aaaa">
         </FooterStyle>
   
      </asp:DataGrid>
 
      <br>
 
      <asp:Label id="Label1" 
           Text="Items in the DataGrid Control: <br>" 
           runat="server"/>
 
   </form>
 
</body>
</html>


Windows 98, Windows 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

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

.NET Framework

Supported in: 2.0, 1.1, 1.0
Show: