Search code examples
winformsdevexpressdecimalxtragriddisplayformat

DevExpress DisplayFormat in RowCellStyle


enter image description hereSwitching application to use DevExpress XtraGrid and implemented customized color and format for row/cells.

For most part formats are being applied correctly. However when applied to a decimal 1000 following format "#,###;(#,###);0" ends up 1000.0000 instead of 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);

 }

Solution

  • For bound columns that do not use CustomColumnDisplayText need to set FormatType before setting DisplayFormatString.

    e.Column.ColumnType 
    

    can show type of the bound property

     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);
    }