2017-03-08 94 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); 
}