Telerik RadGrid:在OnExportCellFormatting中格式化页脚单元格

时间:2013-10-06 03:13:22

标签: telerik radgrid

发布于1天前 嗨,我在radgrid中有一个货币值列,并使用此函数导出到excel以格式化货币单元格

protected void RadGrid_OnExportCellFormatting(object sender, ExportCellFormattingEventArgs e)
{
    if (e.FormattedColumn.DataType == typeof (long))
    {
        e.Cell.Style["mso-number-format"] = "Currency";
    }
}

效果很好,但它不会格式化作为聚合总和值的页脚项。如何将页脚格式化为货币?

1 个答案:

答案 0 :(得分:2)

在标记中(如果您使用的是AJAX控件),您可以按如下方式定义(页脚)聚合格式:

<telerik:GridBoundColumn DataField="columnName" HeaderText="Money" UniqueName="uniqueColumnName"  
    DataFormatString="{0:C}" Aggregate="Sum" FooterAggregateFormatString="{0:C}" />

这将在显示和导出的网格上实现。但是,您可能不希望在屏幕上显示此内容;在这些实例中,您可以从导出事件处理程序中更新属性。您还应注意,从上面的标记中有一个DataFormatString属性,可以格式化单元格中显示的数据。

protected void RadGrid_OnExportCellFormatting(object sender, ExportCellFormattingEventArgs e)
{
    if ((e.FormattedColumn.DataType == typeof(long))) {
        e.Cell.Style("mso-number-format") = "Currency";
        if (((e.FormattedColumn) is GridBoundColumn)) {
            GridBoundColumn col = e.FormattedColumn;
            col.FooterAggregateFormatString = "{0:C}";
        }
    }
} 

否则,您可以使用GridExporting事件处理程序执行此操作:

protected void RadGrid_GridExporting(object sender, GridExportingArgs e)
{
    GridColumn gridCol = grdCustomers.MasterTableView.GetColumnSafe("uniqueColumnName");
    if (((gridCol) is GridBoundColumn)) {
        GridBoundColumn boundCol = (GridBoundColumn)gridCol;
        boundCol.FooterAggregateFormatString = "{0:C}";
    }
}

我确信上面完成的演员等可以更有效/更恰当地实施,但上面的代码应该是一个合理的起点。