How to retrieve value of hidden column on ItemDataBound Event

4 posts, 1 answers
  1. Tomica
    Tomica avatar
    147 posts
    Member since:
    Apr 2008

    Posted 01 Sep 2015 Link to this post

    I have a grid which needs some adjustments to a display column.

     I respond to the ItemDataBound event and retrieve an ID from the row which is my key to the other processing logic.

     I do not want this column to be displayed to the user.

    I set Visible = False and then the value is not returned, just null or an empty string (not sure / don't care)

    Is there a way to get the value when the column is hidden?

    I have tried to set the column width down to 1px but the ID numbers force it wider. 

     

     Tech notes: using latest release of AJAX UI with VS 2015 and C#.

  2. Answer
    Viktor Tachev
    Admin
    Viktor Tachev avatar
    1488 posts

    Posted 03 Sep 2015 Link to this post

    Hello Tomica,

    Note that when the Visible property of a column is set to false it is not rendered. Moreover, the values from that column are not available. In order to retrieve the values and keep the column hidden you have couple of options.

    Add the DataField for the invisible column to the DataKeyNames collection.

    <MasterTableView  DataKeyNames="ID">
     
    . . .
     
    </MasterTableView>

    Then you can use the value like so:

    protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridDataItem)
        {
            GridDataItem dataItem = e.Item as GridDataItem;
     
            var id = dataItem.GetDataKeyValue("ID");
        }
    }


    Alternatively you can replace the Visible property with Display:

    <telerik:GridBoundColumn DataField="ID" HeaderText="ID" UniqueName="ID" Display="false">
    </telerik:GridBoundColumn>


    With this approach you can retrieve the ID like shown below.


    protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridDataItem)
        {
            GridDataItem dataItem = e.Item as GridDataItem;
     
            var id = dataItem["ID"].Text;
        }
    }



    Regards,
    Viktor Tachev
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Tomica
    Tomica avatar
    147 posts
    Member since:
    Apr 2008

    Posted 09 Sep 2015 in reply to Viktor Tachev Link to this post

    Both solutions work for me.
     
    The finer distinctions between "visible=true" and "display=false" remain a mystery to me, it's visible but you cannot actually see <g>
  5. Viktor Tachev
    Admin
    Viktor Tachev avatar
    1488 posts

    Posted 14 Sep 2015 Link to this post

    Hi Tomica,

    The reason for this behavior is because when a column is set as Visible="false" it is not persisted in the ViewState. Thus, the information from that column is not available. This change was introduced in the Q1 2013 release in order to improve performance. You can read more about the change in the following thread.




    Regards,
    Viktor Tachev
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Back to Top