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:
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.)
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
![]()
![]()
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;
}
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