[Mono-dev] DataGridItem.DataItem null after DataBind

Cullen Linn Cullen at LinnLogistics.com
Fri Mar 9 09:39:53 EST 2007

In DataGrid in mono I have noticed that directly after
invocation of DataBind() in a DataGrid, the individual DataGridItems all
provide a null DataItem object. For example, try the following code:

dataGrid.DataSource = someDataSource;

foreach( DataGridItem item in dataGrid.Items ) {
    Console.WriteLine( "DataItem: " + (item.DataItem ==
null?"NULL":item.DataItem.GetType() ); //outputs DataItem: NULL on each

If, however, one is handling one of the item events such as the
ItemCreated event, the DataItem is non null. For Example:

dataGrid.DataSource = someDataSource;
dataGrid.ItemDataBound += new DataGridItemEventHandler( handler );

foreach( DataGridItem item in dataGrid.Items ) {
    Console.WriteLine( "DataItem: " + (item.DataItem == null?"NULL":"OK"
); //outputs DataItem: NULL


private void handler( object sender, DataGridItemEventArgs e ) {
    Console.WriteLine( "DataItem: " + (e.Item.DataItem ==
null?"NULL":"OK" ); //outputs DataItem: OK

I do not believe that this is the correct behavior. Instead, I believe
that the DataItem should be non-null. The cause of the null DataItem
appears to originate in the DataGrid.CreateItem method in the code block
starting at line 858:

if (no_pager && data_bind) {
    res.DataBind ();
    OnItemDataBound (args);
    res.DataItem = null;         <------****HERE

Can anyone tell me why this is set to null? If there is a reason for it,
it is unintuitive to me.

Thanks, Cullen

More information about the Mono-devel-list mailing list