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 Lyons wrote:
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
mi******@4guysfromrolla.com http://www.4GuysFromRolla.com
* When you think ASP.NET, think 4GuysFromRolla.com!