SingingEels : Development Community & Resource

Login

Articles

  • ADO.NET (2)
  • ASP.NET (36)
  • Azure (0)
  • LINQ (5)
  • Security (2)
  • Silverlight (3)
  • SQL (7)
  • Standards (5)
  • WCF (2)

Syndication

  • Articles RSS
  • Blogs RSS

Contribute

  • Our Authors List
  • Member Sign-Up
  • Suggestions Box

GridView intentionally nulls the "DataItem" property

(Aug 28 2007 - 03:28:26 PM by Timothy Khouri) - [print blog post]

I've submitted this issue/bug to Microsoft Connect, but the next version of the framework won't come out for a long time (I'm not talking about 3.5 here). So for those of you who don't already know, here's a tip about the GridView.

The GridView intentionally nulls out the "DataItem" of each row after calling the "OnDataBound" method for that row.

The decompiled code is:

row.DataBind();
   this.OnRowDataBound(e);
   row.DataItem = null;

This is found at the bottom of the "CreateRow" method on the GridView class. Why did the developer go through the trouble of removing the ability for future developers to access this property in a later event (such as PreRender)? Anyway, you can get around it by storing the DataItems into a dictionary (with the "rowIndex" as the key) and use it later.

this.ResultsGrid.RowCreated += delegate(object sender, GridViewRowEventArgs args)
{
   if (args.Row.RowIndex > -1)
   {
       this.dataSourceLookup.Add(args.Row.RowIndex, ((DataRowView)args.Row.DataItem).Row);
   }
};

You can comment about this here, or add feedback to the Microsoft Connect entry here: GridView intentionally nulls "DataItem" after databinding.

You must be logged in to add comments. If you have not already done so, you can create an account here. If you already are a member, you first need to login before you can comment.

Developer / Architect / Author

Blog Archives

  • August 2010 - (1)
  • June 2009 - (1)
  • January 2009 - (1)
  • November 2008 - (1)
  • October 2008 - (2)
  • September 2008 - (2)
  • August 2008 - (3)
  • July 2008 - (1)
  • June 2008 - (3)
  • May 2008 - (2)
  • April 2008 - (2)
  • March 2008 - (4)
  • February 2008 - (2)
  • December 2007 - (2)
  • November 2007 - (1)
  • October 2007 - (4)
  • September 2007 - (9)
  • August 2007 - (7)

Related Ads

SingingEels.com as of Feb 04 2012 - 12:53:59 AM - (0.0468762)