2014-09-25 102 views
1

我有一个文本datagridviewcolumn显示一个整数。
我将format属性设置为colTextDefaultCellStyle.Format = "#,##0";编辑模式下datagridview单元格的格式不同

所以显示的数字得到一个千分位。

在编辑模式下,我不想显示千位分隔符。
但我无法弄清楚如何做到这一点。例如,不起作用:

void dgv_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) 
{ 
    DataGridViewCell cell = dgv.Rows[e.RowIndex].Cells[e.ColumnIndex]; 

    if (e.ColumnIndex == colText.Index) 
    { 
     if (cell.IsInEditMode) cell.Style.Format = ""; 
     else cell.Style.Format = "#,##0"; 
    } 
} 

回答

3

您需要应用实际的格式,然后将e.FormattingApplied设置为True。

void dgv_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) 
{ 
    if (e.ColumnIndex == colText.Index) 
    { 
     DataGridViewCell cell = dgv.Item[e.ColumnIndex, e.RowIndex]; 

     if (cell.IsInEditMode) 
     { 
      e.Value = e.Value.ToString(); 
     } 
     else 
     { 
      e.Value = e.Value.ToString("#,##0"); 
     } 
     e.FormattingApplied = true; 
    } 
} 
+0

的'e.FormattingApplied = TRUE;'是不是在我的情况下必要 – Gerard 2014-10-13 09:59:33

+1

也因为e.Value是在我的情况下,值必须转换的对象'double' /转换为double的方法'的ToString (“#,## 0”)'可用 – Gerard 2014-10-13 10:02:18

相关问题