Expression for Adding Computed column in DataTable with a percentage sign
I've a DataTable
with a column VatRate
retrieved from database column of fl开发者_开发知识库oat
DataType
. Now I want to add a computed DataColumn
in my DataTable
with VatRate formated to two decimal places and a %
sign followed by a space.
So if value in VatRate
column is 12.5
then the value in ComputedColumn
should be 12.50 %
.
NOTE:
I want it on DisplayMember of ComboboxIt would probably be easier to just bind that column to a e.g. DataGrid, and then define on the grid that this particular column should display with a fixed format, e.g. ##0.00 %
.
In general: don't try to "force" your data to look like it should - try to define display properties on the UI for that!
The data is just the data - don't harass it with display options - those belong into the UI and not into your data.
Update:
In ASP.NET, the DropDownList
e.g. has a property called DataTextFormatString
which allows you to define a separate display format for your data.
The Winforms ComboBox
class also has a property called FormatString
that allows you to define how the data should look like.
Update #2: you're using Winforms - so set your FormatString
on the combobox to:
this.comboBox1.FormatString = "#0.00 %";
and then fill in values like 0.0875, 0.125
and so forth - they'll be displayed as 8.75 %
and 12.50 %
respectively.
If formatting would not be an issue, you could have used an expression like this:
myDataColumn.Expression="Convert(VatRate, 'System.String') + '%'"
But why you need additional columns? Can't you simply assing a format string to the binding used to show the data in your From/Window/Page, or use a ToString(...) when writing in some stream?
精彩评论