2014-09-25 100 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

相關問題