2011-09-02 54 views
0

我有一个带有列的ID数据网格,其值为1,2,3,4,5 .... 10,11,12,13 ... 我想要所有的值应该是两个长度,意味着我想要的值为01,02,03 .... 10,11,12 ... 如何做到这一点?如何修复DataGrid单元格中的字符长度

编辑 我正在使用Winforms。

编辑

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.ColumnIndex].DefaultCellStyle = ca; 
      e.FormattingApplied = true; 
     } 
     catch (FormatException) 
     { 
      e.FormattingApplied = false; 
     } 
    } 

这里亚姆能够改变背景色和前景色但文本格式是一样的,不管我做什么。我什至尝试使用String.Format(注释行),但它也不工作。 我也试过你的代码,它不工作。不知道什么是错的。

回答

0

格式化用的ToString字符串( “00”)工作的罚款。

0

(DataGridView中)

对于一个DataGridView,尝试设置的DefaultCellStylethisFormat属性:

this.dataGridView1.Columns["COLUMN_NAME"].DefaultCellStyle.Format = "d2"; 

(WinForms的DataGrid的答案)

在的WinForms,MSDN说: DataGridTextBoxColumn上有Format属性;我不经常使用WinForms来验证这是正确的列类型。


(Web窗体接听)

可以使用DataFormatString属性在列:

DataFormatString="{0:D2}" 
+0

它会在winforms中执行吗? – Sandy

+0

在msdn中添加了WinForms(潜在)答案 –

+0

,他们已经给出了Datagrid的解释,这在Visual Studio 2010中不可用(由DatagridView取代)。因为我对.net很陌生,发现很难破解这个解决方案。还是谢谢 – Sandy

相关问题