I want to format monetary values in my DataGrid
so that they start with the dollar sign ($
).
How can I do this?
If you're talking about showing the data in a monetary format, or even simply adding a "$" to the beginning of data so that
1.49
displays as
$1.49
then you can do this in several ways. On a DataGrid control (web) you can set the DataFormatString property.
One option is to set it to "c" for Currency on a BoundColumn. This will work if your web server's CultureInfo is set to en-US
<asp:BoundColumn DataField="CurrencyValue"
HeaderText="Price"
DataFormatString="{0:c}">
Another option is to use a CustomFormatString, which gives you more refined control over how it's displayed. Say you always want it to be EXACTLY three digits to the right of the decimal and EXACTLY two digits to the left, you can use
{0:$00.000}
More information on custom data format strings for numbers can be found here: http://msdn.microsoft.com/en-us/library/0c899ak8.aspx
Since you're working in WinForms, (sorry, I just re-read the comments) you would use the same technique but apply it to the column's DefaultCellStyle as shown here: http://msdn.microsoft.com/en-us/library/f9x2790s.aspx
dataGridView1.Columns["UnitPrice"].DefaultCellStyle.Format = "c";
Also if you are looking for a Culture-specific cell formatting with a CultureInfo, you can use the FormatProvider Property of DataGridCellStyle. In WinForms:
dataGridView1.Columns["MoneyValue"].DefaultCellStyle.Format = "c";
dataGridView1.Columns["MoneyValue"].DefaultCellStyle.FormatProvider =
System.Globalization.CultureInfo.CreateSpecificCulture("es-CO");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With