Reputation: 105
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;
Upvotes: 0
Views: 2860
Reputation: 1
Basic. Use the datagrid custom format. MM/dd/yyyy
Work like a genie.
Upvotes: -1
Reputation: 136
Try using
dgv.Columns["myColumn"].DefaultCellStyle.Format = "D9";
BEFORE adding data into it.
More info here: Number Format For Datagridview in C#
Upvotes: 3
Reputation: 105
May be this will help
string number = (string)DataGridView1[iCol, iRow].Value;
number = number.PadLeft(8, '0');
DataGridView1[iCol, iRow].Value = number;
Upvotes: 0