DigitalDevGuy
DigitalDevGuy

Reputation: 83

How to display decimal value to 2 decimal places in datatable to display in datagrid

I have a table column sales_price of type money. When I am retrieving its value through a stored procedure and the value is inserted in the datatable dtDetail which is later shown in a datagrid dataListDetail (from a WinForm), it returns the value up to 4 decimal places (because of the type money). I need to set the value up to two decimal places.

private void createTable()
{
    this.dtDetail.Columns.Add("sales_price", System.Type.GetType("System.Decimal"));

    this.dataListDetail.DataSource = this.dtDetail;

    this.dataListDetail.Columns["sales_price"].HeaderText = "Sales Price";

}

After inserting a value like 50 or 50,5 I expect for the sales_price value to display in the datagrid with only 2 decimal places (Ex. 50,00 or 50,50), but it ends displaying it with 4 decimal places (Ex. 50,0000 or 50,5000) by default (due to the type Money).

Thank you in advance!

Upvotes: 0

Views: 4288

Answers (2)

Rovann Linhalis
Rovann Linhalis

Reputation: 621

Just set Format on DefaultCellStyle:

this.dataListDetail.Columns["sales_price"].DefaultCellStyle.Format = "C2";

Upvotes: 1

Glauco953
Glauco953

Reputation: 1

Maybe .ToString("c2") is what you want? It will convert to your systems currency format.

Upvotes: 0

Related Questions