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.