0

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.

2 Answers 2

1

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")).
1
  • Thanks. I had a misunderstanding about the how long the DataSource Object would be around.
    – Steve
    Apr 11, 2012 at 17:12
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.