title: How to access the item being data bound during ItemDataBound?
body: <p>I want to get at the item that is being data bound, during the ItemDataBound event of an asp:repeater.</p>
  
  <p>I tried the following (which was an unaccepted answer in a <a href="https://stackoverflow.com/questions/284420/datagridview-bindinghow-to-color-line-depending-of-the-object-binded#284470">stackoverflow question</a>):</p>
  
  <pre><code>protected void myRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
  {
      Object dataItem = e.Item.DataItem;
      ...
  }
  </code></pre>
  
  <p>but <code>e.Item.DataItem</code> is null.</p>
  
  <p>How can I access the item being data bound during the event called ItemDataBound. I assume the event ItemDataBound happens when an item is being data bound.</p>
  
  <p>I want to get at the object so I can take steps to control how it is displayed, in addition the object may have additional helpful properties to let me enrich how it is displayed.</p>
  
  <h2>Answer</h2>
  
  <p><a href="https://stackoverflow.com/questions/344047/aspnet-how-to-access-the-item-being-data-bound-during-itemdatabound#344073">Tool</a> had the right answer. The answer is that <code>e.Item.Data</code> is only valid when <code>e.Item.ItemType</code> is (Item, AlternatingItem). Other times it is not valid. In my case, I was receiving ItemDataBound events during header (or footer) rows, where there is no DataItem:</p>
  
  <pre><code>protected void myRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
  {
     // if the data bound item is an item or alternating item (not the header etc)
     if (e.Item.ItemType != ListItemType.Item &amp;&amp; 
           e.Item.ItemType != ListItemType.AlternatingItem)
     {
        return;
     }
  
     Object dataItem = e.Item.DataItem;
     ...
  }
  </code></pre>