formatting DataColumn.Expression

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
 
D

Dmitriy Lapshin [C# / .NET MVP]

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.
 
J

J

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
 
D

Dmitriy Lapshin [C# / .NET MVP]

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top