all groups > asp.net datagrid control > october 2004 >
You're in the

asp.net datagrid control

group:

DataGridItem.DataItem property?


DataGridItem.DataItem property? Joel Lyons
10/26/2004 2:09:25 PM
asp.net datagrid control:
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


Re: DataGridItem.DataItem property? Joel Lyons
10/26/2004 2:30:21 PM
Interesting. Thanks Scott!
-Joel

[quoted text, click to view]

Re: DataGridItem.DataItem property? Scott Mitchell [MVP]
10/26/2004 8:23:09 PM
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


[quoted text, click to view]


--

Scott Mitchell
mitchell@4guysfromrolla.com
http://www.4GuysFromRolla.com

AddThis Social Bookmark Button