formatting DataColumn.Expression

  • Thread starter Thread starter J
  • Start date Start date
J

J

Is there anyway to format a calculated datacolumn? I would like the
following column to display as money (formatted as $#,##0.00). Or how about
simply displaying the column formatted as a number with only 2 decimals
(#,##0.00)?
In the sample below, amount and taxrate are existing DataColumn's.

DataColumn colTax = new DataColumn();
colTax.DataType = System.Type.GetType("System.String");
colTax.ColumnName = "tax";
myTable.Columns.Add(colTax);
colTax.Expression = "String.Format(\"{0:C}\",\"amount*taxrate\" )"; //error
: Expression does not support String.Format
 
Hi,

I think the burden of formatting should be put on a presentation tier code
(e.g. the DataGrid control) and not on a DataColumn - it's just not made to
be formattable by itself.
 
The example was formatting as currency, but the principal applies to
unlimited situations.
Don't think of it as formatting for the presentation layer, think of it as
building calculated datacolumns that require use of procedures, functions,
classes, whatever. To build any kind of calculated datacolumn, except for
the most simple, requires this functionality.

Dmitriy Lapshin said:
Hi,

I think the burden of formatting should be put on a presentation tier code
(e.g. the DataGrid control) and not on a DataColumn - it's just not made to
be formattable by itself.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

J said:
Is there anyway to format a calculated datacolumn? I would like the
following column to display as money (formatted as $#,##0.00). Or how about
simply displaying the column formatted as a number with only 2 decimals
(#,##0.00)?
In the sample below, amount and taxrate are existing DataColumn's.

DataColumn colTax = new DataColumn();
colTax.DataType = System.Type.GetType("System.String");
colTax.ColumnName = "tax";
myTable.Columns.Add(colTax);
colTax.Expression = "String.Format(\"{0:C}\",\"amount*taxrate\" )"; //error
: Expression does not support String.Format
 
This seem to be limited by the allowed syntax in the Expression property
(please refer to the "DataColumn.Expression Property" topic in MSDN), which
resembles SQL and has no support for calling managed code.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

J said:
The example was formatting as currency, but the principal applies to
unlimited situations.
Don't think of it as formatting for the presentation layer, think of it as
building calculated datacolumns that require use of procedures, functions,
classes, whatever. To build any kind of calculated datacolumn, except for
the most simple, requires this functionality.

Dmitriy Lapshin said:
Hi,

I think the burden of formatting should be put on a presentation tier code
(e.g. the DataGrid control) and not on a DataColumn - it's just not made to
be formattable by itself.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

J said:
Is there anyway to format a calculated datacolumn? I would like the
following column to display as money (formatted as $#,##0.00). Or how about
simply displaying the column formatted as a number with only 2 decimals
(#,##0.00)?
In the sample below, amount and taxrate are existing DataColumn's.

DataColumn colTax = new DataColumn();
colTax.DataType = System.Type.GetType("System.String");
colTax.ColumnName = "tax";
myTable.Columns.Add(colTax);
colTax.Expression = "String.Format(\"{0:C}\",\"amount*taxrate\" )"; //error
: Expression does not support String.Format
 
Back
Top