首页 文章

ASP.NET / Telerik Webforms:如何拦截和更改RadGrid中列的绑定值?

提问于
浏览
0

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

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

我使用ASP.NET GridView的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;
    }
}

用法:

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

Question: 如何在RadGrid中使用Telerik GridBoundColumn执行等效的 BoundField.GetValue ?如果没有真正的等价物,可用的选项有哪些?

1 回答

  • 0

    使用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 .

相关问题