2017-03-08 110 views
0

enter image description here切換應用程序以使用DevExpress XtraGrid併爲行/單元格實現自定義顏色和格式。DevExpress DisplayFormat in RowCellStyle

對於大多數部分格式正在正確應用。但是,如果將格式爲「#,###;(#,###); 0」的格式應用於小數點後1000,則結果爲1000.0000而不是1,000。

gridView.RowCellStyle += CellFormatting; 
private void CellFormatting(object sender, RowCellStyleEventArgs e)  
{ 
     if (gridView.IsRowSelected(e.RowHandle)) 
     { 
      e.Appearance.BackColor = SystemColors.Highlight; 
      e.Appearance.ForeColor = SystemColors.HighlightText; 
      return; 
     } 

     // get cell by its index 
     var gridRow = gridView.GetRow(e.RowHandle); 
     TLColumn columnEnum = ((BindableTextBoxColumn)e.Column).ColumnEnum; 
     // get new format values 
     T row = (T)gridRow; 

     e.Column.DisplayFormat.FormatString = row.GetCellFormat(columnEnum); 
     e.Appearance.BackColor = row.GetCellBackColor(columnEnum); 
     e.Appearance.ForeColor = row.GetCellColor(columnEnum); 

} 

回答

0

對於不使用CustomColumnDisplayText的綁定列,需要在設置DisplayFormatString之前設置FormatType。

e.Column.ColumnType 

可以顯示綁定屬性的類型

private void CellFormatting(object sender, RowCellStyleEventArgs e) 
{ 
     // get cell by its index 
     var gridRow = gridView.GetRow(e.RowHandle); 
     var column = (BindableTextBoxColumn)e.Column; 
     TLColumn columnEnum = column.ColumnEnum; 
     // get new format values 
     T row = (T)gridRow; 

     e.Column.DisplayFormat.FormatType = (column.IsNumeric) ? FormatType.Numeric : column.DisplayFormat.FormatType; 
     e.Column.DisplayFormat.FormatString = row.GetCellFormat(columnEnum); 
     if (gridView.IsRowSelected(e.RowHandle)) 
     { 
      e.Appearance.BackColor = SystemColors.Highlight; 
      e.Appearance.ForeColor = SystemColors.HighlightText; 
      return; 
     } 
     e.Appearance.BackColor = row.GetCellBackColor(columnEnum); 
     e.Appearance.ForeColor = row.GetCellColor(columnEnum); 
}