views:

614

answers:

1

Hi,

I need to display decimal values in DataGridView

        if (dgvClients.SelectedRows.Count != 0)
        {
            DataGridViewRow row = dgvClients.SelectedRows[0];

            row.Cells["NetAccountValue"].Value = info.DecimalValue;
        }

I need to format value up to given digits after decimal separator.

The problem is that cell Value property stores reference to decimal in my case. When displayed decimal.ToString() is called and default decimal.ToString() produces unformatted string with lots of digits.

One way is to create string and use String.FormatString() to achieve desired formatting and feed it Value instead of decimal.

Is there some other way?

+1  A: 

Using code you can set the DataGridView.DefaultCellStyle property.

You can also do this in the designer by:

  1. Right clicking your DGV and select Edit Columns. The Edit Columns dialog appears.
  2. Select your column on the left side of the dialog and click the DefaultCellStyle property field to bring up the CellStyle Builder.
  3. In the Builder dialog click Format which brings up the Format String Dialog.
  4. Make your selections in this dialog (select Numeric type, which allows specifying number of decimals to display) and save your changes.
Jay Riggs