Kfir
Kfir

Reputation: 105

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;

Upvotes: 0

Views: 2860

Answers (3)

Essential Guide
Essential Guide

Reputation: 1

Basic. Use the datagrid custom format. MM/dd/yyyy

Work like a genie.

Upvotes: -1

Hexzhe
Hexzhe

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

Swarup
Swarup

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

Related Questions