Home > Mobile >  C# WindowsForm DataGridView how to display double variable "0,1" as "0,10"
C# WindowsForm DataGridView how to display double variable "0,1" as "0,10"

Time:01-12

i have a datagridview and a datatable that has a double variable column. I want to display datatable values which are between 0.1 to 0.9 as "0.1,0.2,0.3,0.4.." (i mean with one decimal value) on datagridview. And i want to display values which are greater than 0.9 as "0.10,0.11,0.12,....0.20". But datagridview displays and rounds 0.10, 0.20, 0.30 as 0.1, 0.2, 0.3 when user tries to change datagridview cell value. I know 0.1 and 0.10 are the same values. But i want to display values and allow to user change values as i mentioned.

My table is displayed on datagridview like below;

Data Versions

0,1

0,2

0,6

0,7

0,9

If user tries to increase last cell of column 0,9 to 0,10, datagridview displays 0,10 as 0,1. How can i manage this problem? Please help me.

CodePudding user response:

You can use the DefaultCellStyle property of the column and set the Format property to "0.#" for values between 0.1 and 0.9 or "0.00" if the value is greater than or equal to 0.9 :

foreach (DataGridViewColumn col in dataGridView1.Columns)
{
    if (col.Name == "ColumnName")
    {
        col.DefaultCellStyle.Format = "0.#";
    }
}
  • Related