NullReferenceException with dynamic DataGrid

M

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??
 
M

Mark Gilkes

Sorry!, obviously I meant the NullReferenceException suggests I haven't
declared a reference to the DataGrid.
 
B

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/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,222
Members
46,810
Latest member
Kassie0918

Latest Threads

Top