DataGridItem.DataItem property?

J

Joel Lyons

The help pages make it look like the DataGridItem.DataItem property would
return a row from the bound table. Like so:

//(during setup)
myGrid.DataSource = myDataset.MyTable;
myGrid.DataBind();

//(in OnSelectedIndexChanged)
MyTableRow row = myGrid.SelectedItem.DataItem as MyTableRow;
myLabel.Text = "You selected: " + row.Name;


But this doesn't work! The DataItem property always returns null (so the
cast does also). Am I using this correctly? Is there a different way to
retrieve the DataSet row that was bound to the Grid row?

-Joel
 
S

Scott Mitchell [MVP]

Joel, the DataItem is only not-null during the visit when you call the
DataBind() method. I am assuming when the SelectedIndexChanged event
handler is running, it's on a postback, and you are only binding the
data to the grid on the first page load, no? This is why DataItem is
null in the SelectedIndexChanged event handler.

Typically what is done is the DataKeyField property is set to the
primary key field of the data being displayed in the grid. Then, in the
SelectedIndexChanged event handler, you could get the primary key field,
then hit the database and get the detailed information about this row.

hth


Joel said:
The help pages make it look like the DataGridItem.DataItem property would
return a row from the bound table. Like so:

//(during setup)
myGrid.DataSource = myDataset.MyTable;
myGrid.DataBind();

//(in OnSelectedIndexChanged)
MyTableRow row = myGrid.SelectedItem.DataItem as MyTableRow;
myLabel.Text = "You selected: " + row.Name;


But this doesn't work! The DataItem property always returns null (so the
cast does also). Am I using this correctly? Is there a different way to
retrieve the DataSet row that was bound to the Grid row?


--

Scott Mitchell
(e-mail address removed)
http://www.4GuysFromRolla.com

* When you think ASP.NET, think 4GuysFromRolla.com!
 
J

Joel Lyons

Interesting. Thanks Scott!
-Joel

Scott Mitchell said:
Joel, the DataItem is only not-null during the visit when you call the
DataBind() method. I am assuming when the SelectedIndexChanged event
handler is running, it's on a postback, and you are only binding the data
to the grid on the first page load, no? This is why DataItem is null in
the SelectedIndexChanged event handler.

Typically what is done is the DataKeyField property is set to the primary
key field of the data being displayed in the grid. Then, in the
SelectedIndexChanged event handler, you could get the primary key field,
then hit the database and get the detailed information about this row.

hth





--

Scott Mitchell
(e-mail address removed)
http://www.4GuysFromRolla.com

* When you think ASP.NET, think 4GuysFromRolla.com!
 

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

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top