Home All Groups Group Topic Archive Search About

NullReferenceException with dynamic DataGrid

Author
29 Sep 2006 4:06 PM
Mark Gilkes
I have a page using a dynamically added DataGrid. DataGrid contains a
TemplateColumn and is bound to a DataTable in a protected method called from
OnInit. ItemCommand and SortCommand events are wired and working. When I then
moved the DataGrid into a Table, Row, Cell and Panel control to format the
output I get a null reference exception suggesting I have declared a
reference to the DataGrid. This is clearly not the case. When I catch the
error the DataGrid is rendered properly. Can someone explain this phenomina?
Everthing is the same apart from the DataGrid is now nested deeper in the
Control hierarchy??
--
MG

Author
29 Sep 2006 4:19 PM
Mark Gilkes
Sorry!, obviously I meant the NullReferenceException suggests I haven't
declared a reference to the DataGrid.
--
MG


Show quoteHide quote
"Mark Gilkes" wrote:

> I have a page using a dynamically added DataGrid. DataGrid contains a
> TemplateColumn and is bound to a DataTable in a protected method called from
> OnInit. ItemCommand and SortCommand events are wired and working. When I then
> moved the DataGrid into a Table, Row, Cell and Panel control to format the
> output I get a null reference exception suggesting I have declared a
> reference to the DataGrid. This is clearly not the case. When I catch the
> error the DataGrid is rendered properly. Can someone explain this phenomina?
> Everthing is the same apart from the DataGrid is now nested deeper in the
> Control hierarchy??
> --
> MG
Author
29 Sep 2006 11:22 PM
Brennan Stehling
What you should check is whether or not the DataItem is null prior to
accessing it.  It will be null after a PostBack and also for Header,
Footer and non-Item rows for the DataGrid.  You can check the type of
the row to ensure it will have a defined DataItem value.

You can read about it here...

http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.datagriditem.itemtype.aspx

The rows which have the DataItem defined are the Item and
AlternatingItem rows.

Ultimately you can resolve your issue and see what is happening with
the debugger.  I wrote up some instructions to do that here...

http://brennan.offwhite.net/blog/2006/09/12/debugging-aspnet-20-with-visual-studio-2005/

Brennan Stehling
http://brennan.offwhite.net/blog/

Mark Gilkes wrote:
Show quoteHide quote
> Sorry!, obviously I meant the NullReferenceException suggests I haven't
> declared a reference to the DataGrid.
> --
> MG
>
>
> "Mark Gilkes" wrote:
>
> > I have a page using a dynamically added DataGrid. DataGrid contains a
> > TemplateColumn and is bound to a DataTable in a protected method called from
> > OnInit. ItemCommand and SortCommand events are wired and working. When I then
> > moved the DataGrid into a Table, Row, Cell and Panel control to format the
> > output I get a null reference exception suggesting I have declared a
> > reference to the DataGrid. This is clearly not the case. When I catch the
> > error the DataGrid is rendered properly. Can someone explain this phenomina?
> > Everthing is the same apart from the DataGrid is now nested deeper in the
> > Control hierarchy??
> > --
> > MG