Skip to main content

How to: Format Display Text in Grid's Cells Depending on Values in Other Cells

  • 2 minutes to read

The following code snippet handles the CustomColumnDisplayText event to format prices based on a currency type column.

The currency type column contains only 0 and 1 values. If the currency type equals 0, the price is displayed in US dollars. If the currency type equals 1, the price is displayed in euros.

Display Custom Text with an Event - WinForms Data Grid, DevExpress

using DevExpress.XtraGrid.Views.Base;
using DevExpress.XtraGrid;
using System.Globalization;

// Culture settings for currency formatting
CultureInfo ciUSA = new CultureInfo("en-US");
CultureInfo ciEUR = new CultureInfo("fr-FR", false);

private void gridView1_CustomColumnDisplayText(object sender, CustomColumnDisplayTextEventArgs e) {
    ColumnView view = sender as ColumnView;

    // Process only valid data rows and the Price column.
    if (e.Column.FieldName != "Price" || e.ListSourceRowIndex == GridControl.InvalidRowHandle)
        return;

    int currencyType = (int)view.GetListSourceRowCellValue(e.ListSourceRowIndex, "CurrencyType");

    decimal price = Convert.ToDecimal(e.Value);

    // Select format based on currency type.
    e.DisplayText = currencyType == 0
        ? string.Format(ciUSA, "{0:c}", price)
        : string.Format(ciEUR, "{0:c}", price);
}