Telerik RadGrid: Formatting Footer cell in OnExportCellFormatting

ⅰ亾dé卋堺 提交于 2019-12-11 12:57:57

问题


Posted 1 day ago Hi, I have a currency value column in a radgrid, and exporting to excel using this function for formatting the currency cells

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

Works very well, but it doesn't format the footer item which is an aggregated sum value. How do I format the footer to be currency as well?


回答1:


In the markup (if you are using the AJAX controls) you can define the (footer) aggregate format as follows:

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

This will be implemented on the displayed and exported grid. It is however possible that you do not want to display this on the screen; in these instances you could update the attribute from within an export event handler. You should also note that from the above markup there is a DataFormatString attribute which can format the data displayed in the cell.

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

You could otherwise do this with a GridExporting event handler:

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

I'm sure the casting etc. done above could be implemented more efficiently/properly, but the above code should be a reasonable place to start from.



来源:https://stackoverflow.com/questions/19205036/telerik-radgrid-formatting-footer-cell-in-onexportcellformatting

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!