问题
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;
回答1:
Try using
dgv.Columns["myColumn"].DefaultCellStyle.Format = "D9";
BEFORE adding data into it.
More info here: Number Format For Datagridview in C#
回答2:
May be this will help
string number = (string)DataGridView1[iCol, iRow].Value;
number = number.PadLeft(8, '0');
DataGridView1[iCol, iRow].Value = number;
来源:https://stackoverflow.com/questions/42906479/leading-zero-in-datagridview