Leading zero in DataGridView

陌路散爱 提交于 2019-12-25 14:48:55

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!