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

Accessing RadGrid entity datasource's field values in code behind

1 Answer 549 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Steve
Top achievements
Rank 1
Steve asked on 05 Jul 2011, 07:25 PM
This seems like it should be a straight forward thing to do, but it's eluding me, and I've searched for an answer. Hopefully I'm overlooking something simple.

I'm binding my RadGrid to an entity data source. I'm using GridTemplateColumn columns for many of the columns, as I combine several of the data fields into one column. How do I access the RadGrid datasource's field values (in the code-behind)? This is in non-edit, regular view of the grid.

For example, I'd like to do something like this:
protected void ContactsGrid_ItemDataBound(object sender, GridItemEventArgs e)
{
    if ((!e.Item.IsInEditMode) && (e.Item is GridDataItem))
    {
        GridDataItem dataItem = (GridDataItem)e.Item;
 
        // Assuming my entity data source has a field named "dataFieldName", would be nice to access:
        string dataFieldValue = (string)dataItem("dataFieldName").Value;
        // Would then find the GridTemplateColumn in order to fill in the ItemTemplate, 
        // which already know how to do...
    }
}


Thanks,
Steve

1 Answer, 1 is accepted

Sort by
0
Accepted
Tsvetoslav
Telerik team
answered on 06 Jul 2011, 04:37 PM
Hello Steve,

You should get the values as follows:

protected void ContactsGrid_ItemDataBound(object sender, GridItemEventArgs e)
{
    if ((!e.Item.IsInEditMode) && (e.Item is GridDataItem))
    {
        GridDataItem dataItem = (GridDataItem)e.Item;
        Hashtable fieldValues = new Hashtable();
        dataItem.ExtractValues(fieldValues);
        string dataFieldValue = fieldValues["dataFieldName"].ToString();
        // Would then find the GridTemplateColumn in order to fill in the ItemTemplate,
        // which already know how to do...
    }
}

Hope it helps. 

Greetings,
Tsvetoslav
the Telerik team

Register for the Q2 2011 What's New Webinar Week. Mark your calendar for the week starting July 18th and book your seat for a walk through of all the exciting stuff we will ship with the new release!

Tags
Grid
Asked by
Steve
Top achievements
Rank 1
Answers by
Tsvetoslav
Telerik team
Share this question
or