This is a migrated thread and some comments may be shown as answers.

Inherited column footer format

3 Answers 191 Views
Grid
This is a migrated thread and some comments may be shown as answers.
christina noel
Top achievements
Rank 1
christina noel asked on 16 Aug 2011, 05:18 PM
My site has a lot of grids for which we do specialized formatting, especially for date/time and currency data. Although it works to use GridTemplateColumns and use Eval statements to do the special formatting, it's becoming increasingly unwieldy to write all those template columns. Therefore, we have created a column class inherited from GridBoundColumn. The primary purpose of this column is to override FormatDataValue with our custom formatting:

protected override string FormatDataValue(object dataValue, GridItem item)
{
    return FormatString(dataValue, DataBinder.GetDataItem(item));
}
 
protected virtual string FormatString(object dataValue, object DataItem)
{
    if (dataValue != null)
    {
        String lc = LanguageCodeField.HasValue()
                            ? DataBinder.Eval(DataItem, LanguageCodeField).ToString()
                            : String.Empty;
        CultureInfo ci = lc.HasValue()
                                 ? new CultureInfo(lc)
                                 : CultureInfo.CurrentCulture;
 
        if(DataFormatString.HasValue())
        {
            return String.Format(ci, DataFormatString, dataValue);
        }
        if (dataValue is DateTime)
        {
            return Dates.GetShortDateTimeString(dataValue as DateTime?);
        }
        if(IsCurrency)
        {
            double dv = Convert.ToDouble(dataValue);
 
            return Cultures.FormatCurrencyValue(lc, dv);
        }
 
        return String.Format(ci, "{0}", dataValue);
       
    }
    return String.Empty;
}
Note: HasValue = !String.IsNullOrEmpty; and Dates.GetShortDateTimeString and Cultures.FormatCurrencyValue are our standard formatting functions for dates and currencies, respectively. LanguageCodeField is a property that we added to our column in order to allow our DataTable to specify different currencies/date formats on a row-by-row basis, and we assume that it will eval to culture strings like "en-US".

Our big problem now is that the Grid Footers aren't applying our formatting. They apparently don't run through FormatDataValue. Here's the code for our column and the GridBoundColumn and I've attached a screen shot that shows the difference when LangaugeCodeField evaluates to "en-GB" even though my computer's culture is the US. (Note: We only enable the footer when LanguageCodeField is the same for all rows -- that's not an issue. If we need to aggregate it, too, we can use "first" with no problem.)
<cc2:MyBoundColumn HeaderText="[Total New]" UniqueName="TOTALEXPENSECALC2" DataField="TOTALEXPENSE" Resizable="false" Reorderable="false"
     LanguageCodeField="LANGUAGECODE" Aggregate="Sum" FooterStyle-CssClass="textbold" FooterText="Page Total " />
<telerik:GridBoundColumn HeaderText="[Total Standard]" UniqueName="TOTALEXPENSECALC3" DataField="TOTALEXPENSE" Resizable="false" Reorderable="false"
      Aggregate="Sum" FooterStyle-CssClass="textbold" FooterText="Page Total " DataFormatString="{0:C}"  />

As you can see in the screen shot, the "Footer Text" property and CssClass are applying properly (Yay!), but we need the Total itself to be formatted in the correct currency. Please help us figure out what to override or what event we have to tie into in order to make custom formatting work for the footer of our inherited column.

--Christina

3 Answers, 1 is accepted

Sort by
1
Shinu
Top achievements
Rank 2
answered on 17 Aug 2011, 07:34 AM
Hello Christina,

Try setting the  FooterAggregateFormatString  as shown below.
aspx:
<telerik:GridBoundColumn HeaderText="[Total Standard]" UniqueName="TOTALEXPENSECALC3" DataField="TOTALEXPENSE" Resizable="false" Reorderable="false"
Aggregate="Sum" FooterStyle-CssClass="textbold" FooterAggregateFormatString="Total : {0:£ ###,##0.00}" FooterText="Page Total " DataFormatString="{0:c}"   />

Thanks,
Shinu.
0
christina noel
Top achievements
Rank 1
answered on 17 Aug 2011, 03:27 PM
I don't know the language code at design time, since the user can choose to view all currencies (without a grid footer) or can pick a currency -- which affects the dataset that gets bound to the grid and shows the footer.

I could maybe do it this way if I set FooterAggregateFormatString programmatically - but I can't see how to get the format string out of the CultureInfo object. All the examples I see just pass the CultureInfo object into .Format or .ToString rather than extracting the formatting string.

Can I temporarily set the culture info object used by the grid or the column to something other than the system culture?

--Christina
0
Accepted
Radoslav
Telerik team
answered on 23 Aug 2011, 09:30 AM
Hi Christina,

The footer's cells formatting is made into the private FormatCellText method and you could not override it. However you could achieve the desired functionality by handling the RadGrid.ItemDataBound event and format the value form the footer cell:
void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
      if (e.Item is GridFooterItem)
      {
           GridFooterItem item = e.Item as GridFooterItem;
           decimal o = Convert.ToDecimal(item["Price"].Text.Split(new char[] {':'})[1]);
           CultureInfo ci = new CultureInfo("bg-BG");
           item["Price"].Text = o.ToString("c", ci);
      }
}

Additionally I am sending you a simple example, please check it out and let me know if it helps you.

Looking forward for your reply.

Kind regards,
Radoslav
the Telerik team

Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>

Tags
Grid
Asked by
christina noel
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
christina noel
Top achievements
Rank 1
Radoslav
Telerik team
Share this question
or