0

I am having an issue with using DataTextFormatString property where the values are not actually being formatted when they are being bound to the drop down list.

dvWaterLimits = this._lookupProvider.GetDataTable(version, form, package, state, DateTime.Today(), Zone);
this.ddlMyDropDown.DataSource = dvWaterLimits;
this.ddlMyDropDown.DataTextField = "LimitDescription";
this.ddlMyDropDown.DataValueField = "TotalLimit";
this.ddlMyDropDown.DataTextFormatString = " {0:$###,###,##0} ";
this.ddlMyDropDown.DataBind();

The data table I am getting back looks like the following: Where the second column is the TotalLimit and the fifth column is LimitDescription

1133    5000    2   3   $5000 
1133    10000   2   3   $10000 
1133    15000   2   3   $15000 
1133    20000   2   3   $20000 
1133    25000   2   3   $25000 

And the Drop down looks just like the fifth column when it should look like $5,000 $10,000... etc. with the commas.

leppie
  • 115,091
  • 17
  • 196
  • 297
Jamie Babineau
  • 746
  • 2
  • 12
  • 25

3 Answers3

2

use C in your format string.

{0:C}

C or c Displays numeric values in currency format. You can specify the number of decimal places.

Note: a comma is not used in US for decimals!

Caspar Kleijne
  • 21,552
  • 13
  • 72
  • 102
  • 1
    I _always_ forget that I can specify the precision with as `"C0"` so instead of setting `CurrencyDecimalDigits` property :( – Soner Gönül Dec 15 '15 at 13:57
1

One option can be override your CurrentCulture for this operation with cloned en-US culture which setted CurrencyDecimalDigits to 0 and use The "C" format specifier for DataTextFormatString property as "{0:C}".

It is unclear you want as $5,000 or $5000. If you don't want group separator, you need to assign CurrencyGroupSeparator to empty string as well.

As an example;

int i = 5000;
var clone = (CultureInfo)CultureInfo.GetCultureInfo("en-US").Clone();
clone.NumberFormat.CurrencyDecimalDigits = 0;
Console.WriteLine(i.ToString("C", clone)); // $5,000

or

int i = 5000;
var clone = (CultureInfo)CultureInfo.GetCultureInfo("en-US").Clone();
clone.NumberFormat.CurrencyDecimalDigits = 0;
clone.NumberFormat.CurrencyGroupSeparator = "";
Console.WriteLine(i.ToString("C", clone)); // $5000

Or much better without setting CurrencyDecimalDigits property;

int i = 5000;
Console.WriteLine(i.ToString("C0", CultureInfo.GetCultureInfo("en-US"))); // $5,000

I honestly don't know how to override a culture for just one element in asp.net (or possible or not) but you can check these topics;

Community
  • 1
  • 1
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
0

So not sure if it was the '$', or if the value was coming in as a string so it couldn't make the match between the # and the value coming in. But this started working after i made this ineffecient change.

foreach (DataRow dr in dtClone.Rows)
            {
                if (dr["LimitDescription"].ToString().Contains("$"))
                {
                    dr["LimitDescription"] = Convert.ToDouble(dr["LimitDescription"].ToString().Replace("$", "")).ToString("$###,###,##0");
                }
            }
Jamie Babineau
  • 746
  • 2
  • 12
  • 25