This is a migrated thread and some comments may be shown as answers.

How to retrieve value of hidden column on ItemDataBound Event

3 Answers 948 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Tomica
Top achievements
Rank 2
Tomica asked on 02 Sep 2015, 01:44 AM

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#.

3 Answers, 1 is accepted

Sort by
0
Accepted
Viktor Tachev
Telerik team
answered on 03 Sep 2015, 04:38 PM
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
0
Tomica
Top achievements
Rank 2
answered on 10 Sep 2015, 02:10 AM
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>
0
Viktor Tachev
Telerik team
answered on 14 Sep 2015, 07:58 AM
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
Tags
Grid
Asked by
Tomica
Top achievements
Rank 2
Answers by
Viktor Tachev
Telerik team
Tomica
Top achievements
Rank 2
Share this question
or