I have a datagrid with a column, ID with values like 1,2,3,4,5....10,11,12,13... I want all the values should be of length two, means i want the values as 01,02,03....10,11,12... How to do it?
EDIT I am using Winforms.
EDIT
if (dg.Columns[e.ColumnIndex].Name == "Id")
{
try
{
//e.Value = String.Format("{0:00.0}", e.Value);
DataGridViewCellStyle ca = new DataGridViewCellStyle();
ca.ForeColor = System.Drawing.Color.Red;
ca.Format = "d2";
dg.Columns[e.Co开发者_开发技巧lumnIndex].DefaultCellStyle = ca;
e.FormattingApplied = true;
}
catch (FormatException)
{
e.FormattingApplied = false;
}
}
Here iam able to change backcolor and forecolor but the text format remains the same, no matter what i do. I even tried using String.Format (commented line) but its not working either. I tried your code also, its not working. don't know whats wrong.
(DataGridView)
For a DataGridView, try setting the Format
property of the DefaultCellStyle
like this:
this.dataGridView1.Columns["COLUMN_NAME"].DefaultCellStyle.Format = "d2";
(WinForms DataGrid answer)
In WinForms, MSDN says there's a Format property on the DataGridTextBoxColumn; I don't use WinForms often enough to verify that that's the correct type of column.
(Web Forms answer)
You can use the DataFormatString property on the column:
DataFormatString="{0:D2}"
Formatting the string with ToString("00") worked fine.
精彩评论