Tech Off Thread

14 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

getting dataitem for gridview row on postback

Back to Forum: Tech Off
  • User profile image
    JParrish

    Is there a way to get to the dataitem for a gridview row, when a postback is made from outside of the gridview itself? I have checkboxes that I am using to indicate selection, and want to be able to get to the object I use to bind the gridview for each row that is checked when a button is pressed. Thanks for info, I'm still a grid newb! Tongue Out

  • User profile image
    odujosh

    Assuming ASP.NET:
    You can put a Templated column in the gridview Columns collection and put a checkbox control in the column. There are two hooks (I can name off the top of my head) on post back:
    -Set Checkbox to auto post back (or use AJAX). Then you can access the Gridview Row by the ParentControl Property during the Selected Event of the Checkbox.
    -Foreach through each gridviewrow at a more convient post back back time. Test for the Selected property of the check box. Do work based on that.

    Later Josh

  • User profile image
    JParrish

    odujosh wrote:
    
    -Foreach through each gridviewrow at a more convient post back back time. Test for the Selected property of the check box. Do work based on that.


    That is what I am doing, unfortunately the checkbox and the template column have no attribute that I can use to just store a data value, in winforms most components have a "Tag" attribute that you can throw some data into.

    I am resorting to adding a hidden column and trying to get the data back from it. Thanks

  • User profile image
    odujosh

    I'll get you some source tommorow showing you what I mean.

  • User profile image
    LostIn​Tangent

    odujosh wrote:
    Assuming ASP.NET:
    You can put a Templated column in the gridview Columns collection and put a checkbox control in the column. There are two hooks (I can name off the top of my head) on post back:
    -Set Checkbox to auto post back (or use AJAX). Then you can access the Gridview Row by the ParentControl Property during the Selected Event of the Checkbox.
    -Foreach through each gridviewrow at a more convient post back back time. Test for the Selected property of the check box. Do work based on that.

    Later Josh


    The first option wouldn't be viable in most cases, since you would be saving the user's selection as they actually check the checbox, as opposed to hitting a save or apply button, and in terms of useability, that would be confusing to most.

    The second option is the method I use. And Josh, as you were saying about resorting to the HiddenField to store the key value, I'm not sure if there is a truly BETTER way of accomplishing that task. Even in Windows development, using the Tag property is still a hack.

  • User profile image
    odujosh

    Just to be clear. You know which data row the check box is associated with. You can access any data pulled with the data item that is bound to the Data Row pretty easily. I'll give some source tommorow when I have access to my source repository.

    If you are not pulling the data down in the datasource you are using for the grid view. Then you do have problem. Just because the Data isn't bound to gridview row child control (like a label or hidden field) doesn't mean you can't work with it on the server. The Grid View Row gives you full access to the data item bound to the gridview row regardless of whether it is bound to any child control

  • User profile image
    LostIn​Tangent

    odujosh wrote:
    

    Just to be clear. You know which data row the check box is associated with. You can access any data pulled with the data item that is bound to the Data Row pretty easily.



    How would the CheckBox know what data it was associated with? The data controls (GridView, Repeater, ect) individual items (GridViewRow, RepeaterItem) don't maintain their DataItem in viewstate, so besides accessing it in an event like ItemDataBound, you'd have to reretreive it from the database. But in order to do that you'd have to know some way to associate the individual items in the list to an actual record in a database.

  • User profile image
    LostIn​Tangent

    Alright Josh I apologize for being an idiot. If you're using a Repeater, then yes the HiddenField solution is a decent option, but if you're using the GridView, you can use the DataKeyNames property to specify a column (or columns) that will be stored along with row in the grid and maintained in viewstate.

    So as you're looping through the GridViewRows on postback, you just grab the DataKey for the current row which will be holding the key that associates it with a data record.

    I really wish they would have implemented that similiar functionality in the Repeater.

  • User profile image
    JParrish

    Ok.. so DataKey is similar to how I used the Tag property. Big Smile I really like that the object driving the binding is exposed during the binding events as the dataitem for a row. I wish it was optional to persist that to viewstate so that it was instrinsicly exposed for each row in the rows collection, after a post back from other components. Thanks all for the help, I will take a look at using the datakey property tomorrow.

  • User profile image
    JParrish

    Worked well. It is interesting that the grid will build that collection for you. The property DataKeyNames is a string array that would lead you to believe you have to manage the values that go into it, but I suppose that wouldn't make much sense since you could create an index keyed string array while binding and throw that into viewstate to serve the same purpose. Anyway, I'm off and running to stumble into my next issue! Big Smile

  • User profile image
    LostIn​Tangent

    Glad to hear Smiley Let me know if you have any other questions.

    It's cool to see a fellow Floridian niner. I'm from Sarasota myself. By the way, are you going to DevConnections in Orlando in March?

  • User profile image
    odujosh

    foreach (GridViewRow R in GridView1.Rows)

    {
    if(((CheckBox)R.FindControl("chk")).Checked)

    { //If checkbox is checked do work
    DataRow DR = (DataRow)R.DataItem;
    //or
    //R.DataItemIndex //Then find the row that way.
    }


    }

    You can do this anytime after the gridview is created and bound. As you can see the fields the gridview binds to does not limit your access to the data pulled down.

    If you use a business object through a Object Data Source you would cast the DataItem to that.

  • User profile image
    LostIn​Tangent

    The DataItem property isn't persisted in ViewState so that is of no use to you when trying to process the grid rows in a postback event and you can't rely on the DataItemIndex because that's just telling you the index of the row that was bound to the grid item, which has nothing to do with the primary key value that identifies the row in the underlying data table.

    The DataKey approach is by far the best solution when using a GridView, and is preferred with all developers I've spoken with. It allows you to acheive this functionality even in tables that have multiple primary keys.

  • User profile image
    odujosh

    Ah I 'll have to try that sometime.

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.