ASP.NET / Telerik Webforms: How do you intercept and change the bound value of a column in a RadGrid?

969 views Asked by At

I have some custom conversion and formatting to do for certain data on multiple Telerik RadGrids throughout a web application. Prior to using Telerik, I used the standard ASP.NET GridView. To do my custom conversion and formatting while using the GridView, I derived a new class from BoundField and overrode the FormatDataValue and GetValue methods. In this manner, I could intercept the value for a grid cell from the bound data source, change the value as the grid sees it, and apply my custom formatting rules. Then whenever I needed this functionality for a particular grid column, I used my custom BoundField.

I do not see any method for the Telerik GridBoundColumn to override and intercept the point at which the grid fetches data from the bound data source (the equivalent to the GridView's BoundField.GetValue method). I need the cell data to appear to the Telerik grid to be value B for all functions - display, sorting, filtering, grouping, etc. - even though the corresponding data in the data source is value A, and I cannot alter the underlying data source in any way.

Code snippets for what I was doing using the ASP.NET GridView's BoundField:

public class MyBoundField
    : BoundField
{
    protected override string FormatDataValue(object dataValue, bool encode)
    {
        if ( someCondition )
        {
            return MyFormatFunction(dataValue, encode);
        }
        else
        {
            return base.FormatDataValue(dataValue, encode);
        }
    }

    protected override object GetValue(Control controlContainer)
    {
        // Get the data bound value.
        object boundValue = base.GetValue(controlContainer);

        // Convert the value for the grid's usage.
        object convertedValue = MyConversionFunction(boundValue);

        return convertedValue;
    }
}

Usage:

<asp:GridView ... >
    <asp:BoundField .... />
    <custom:MyBoundField .... />
</asp:GridView>

Telerik option:

public class MyGridBoundColumn
    : GridBoundColumn
{
    protected override string FormatDataValue(object dataValue, GridItem item)
    {
        if ( someCondition )
        {
            return MyFormatFunction(dataValue, item);
        }
        else
        {
            return base.FormatDataValue(dataValue, item);
        }
    }

    // Override what method in order to convert the value for all grid functionality????
}

Question: How do I do the equivalent of BoundField.GetValue with a Telerik GridBoundColumn within a RadGrid? If there is no true equivalent, what are the available options?

1

There are 1 answers

2
rdmptn On

Use the OnItemDataBound event and modify the cell directly as shown here: http://www.telerik.com/help/aspnet-ajax/grid-accessing-cells-and-rows.html.

Let's assume you have this markup:

        <telerik:RadGrid ID="RadGrid1" runat="server" OnItemDataBound="RadGrid1_ItemDataBound">
            <MasterTableView>
                <Columns>
                    <telerik:GridBoundColumn UniqueName="first" DataField="Notification"></telerik:GridBoundColumn>
                </Columns>
            </MasterTableView>
        </telerik:RadGrid>

where the Notification field is present in your data source. Here is a sample server handler:

protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
    if (e.Item is GridDataItem)
    {
        GridDataItem dataItem = e.Item as GridDataItem;
        dataItem["first"].Text = dataItem["first"].Text + DateTime.Now.ToString();
    }
}

Note how the UniqueName of the column is used in the server code.