Search code examples
c#winformsdatagridview

Leading zero in DataGridView


I want to display a number with a leading zero in a DataGridView but i don't know how.

for example:

3910323 → 03910323

I've tried to add a leading zero to an int/string in a DataGridView and for some reason the DataGridView emits the zero.

How can i format the cells in a way that a leading zero will be displayed?

This is my code:

string number = dgvCustomers[0, 0].Value.ToString();
number = "0" + number;
dgvCustomers[0, 0].Value = number;

Solution

  • Try using

    dgv.Columns["myColumn"].DefaultCellStyle.Format = "D9";
    

    BEFORE adding data into it.

    More info here: Number Format For Datagridview in C#