Reputation: 35
I've got a gridview bound to an ObjectDataSource including a checkbox to select an item in the grid.
In the CheckChanged event I've got some code like this:
//Clear the existing selected row
foreach (GridViewRow oldrow in uxRaceList.Rows)
{
var otherOpt = (RadioButton)oldrow.FindControl("rdbRaceNum");
if (otherOpt != sender)
otherOpt.Checked = false;
}
//Set the new selected row
RadioButton rb = (RadioButton)sender;
GridViewRow row = (GridViewRow)rb.NamingContainer;
((RadioButton)row.FindControl("rdbRaceNum")).Checked = true;
Now that I have a reference to the GridViewRow, is it possible for me to get at my OrigionalDataSourceObject?
I know I can get at the data displayed in the gird:
_selectedRaceNum = Convert.ToInt32(rb.Text.Substring(0, 1));
But I want something like:
var odsMyobject = row.DataItem as MyCustomObject;
I know I can store an ID int he grid and use that to look back at my database to get the data, but I want to avoid another roundtrip to the data.
Perhaps I can somehow ask the ObjectDataSource for the object?
Thanks.
Upvotes: 0
Views: 1872
Reputation: 10095
Your Question - Now that I have a reference to the GridViewRow, is it possible for me to get at my OrigionalDataSourceObject?
You GridView
DataSource Object
will be Disposed
by End of the Page Life Cycle
. You can check it in your Quick Watch
during Postback
once the Page
is Loaded.
Your Question - I know I can store an ID int he grid and use that to look back at my database to get the data, but I want to avoid another roundtrip to the data.
You have an Option to keep the data in ViewState
/ Session
during the Postback
. In case it's about keeping the data in the same page only, then only ViewState
should be considered.
Your Question - But I want something like:
var odsMyobject = row.DataItem as MyCustomObject;
You can use DataKeys
in GridView
and you can easily access it using the RowIndex
which you have already calculated using NamingContainer
. Another Option/Alternative is to Hide the Control and Bind
it with your Property
and Access this Control like below.
((ControlType)row.FindControl("ControlID")).
Upvotes: 1
Reputation: 13579
I might be misinterpreting your question, but if you want the data object bound to that particular GridViewRow, you should be able to just use the DataItem property to get it.
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridviewrow.dataitem.aspx
Gets the underlying data object to which the GridViewRow object is bound.
Upvotes: 0