0

在此处输入图像描述切换应用程序以使用 DevExpress XtraGrid 并为行/单元格实现自定义颜色和格式。

大多数情况下,格式都得到了正确应用。但是,当应用于十进制 1000 时,格式为 "#,###;(#,###);0" 以 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);

 }
4

1 回答 1

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);
}
于 2017-03-08T21:52:11.210 回答