【问题标题】:ASP.NET / Telerik Webforms: How do you intercept and change the bound value of a column in a RadGrid?ASP.NET / Telerik Webforms:如何截取和更改 RadGrid 中列的绑定值?
【发布时间】:2014-11-20 15:23:57
【问题描述】:

我对整个 Web 应用程序中多个 Telerik RadGrids 上的某些数据进行了一些自定义转换和格式化。在使用 Telerik 之前,我使用了标准的 ASP.NET GridView。为了在使用GridView 时进行自定义转换和格式化,我从BoundField 派生了一个新类并覆盖了FormatDataValueGetValue 方法。通过这种方式,我可以从绑定的数据源中截取网格单元格的值,在网格看到它时更改值,并应用我的自定义格式规则。然后,每当我需要对特定网格列使用此功能时,我都会使用我的自定义 BoundField

我没有看到 Telerik GridBoundColumn 的任何方法来覆盖和拦截网格从绑定数据源获取数据的点(相当于 GridView 的 BoundField.GetValue 方法)。 我需要单元格数据在 Telerik 网格中显示为所有功能的值 B - 显示、排序、过滤、分组等 - 即使数据源中的相应数据是值 A,我也无法更改以任何方式的底层数据源。

使用 ASP.NET GridView 的 BoundField 编写代码 sn-ps:

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;
    }
}

用法:

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

Telerik 选项:

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????
}

问题: 如何在 RadGrid 中使用 Telerik GridBoundColumn 执行相当于 BoundField.GetValue 的操作?如果没有真正的等价物,有哪些可用选项?

【问题讨论】:

    标签: asp.net gridview webforms telerik radgrid


    【解决方案1】:

    使用 OnItemDataBound 事件直接修改单元格,如下所示:http://www.telerik.com/help/aspnet-ajax/grid-accessing-cells-and-rows.html

    假设你有这个标记:

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

    通知字段在您的数据源中的位置。这是一个示例服务器处理程序:

    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();
        }
    }
    

    注意列的 UniqueName 在服务器代码中是如何使用的。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多