Skip to main content

DevExpress v24.2 Update — Your Feedback Matters

Our What's New in v24.2 webpage includes product-specific surveys. Your response to our survey questions will help us measure product satisfaction for features released in this major update and help us refine our plans for our next major release.

Take the survey Not interested

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

  • 2 minutes to read

The following example shows how to dynamically customize the cell’s display text by handling the ColumnView.CustomColumnDisplayText event.

The grid View in the example contains the Currency Type and Price columns. If the currency type is equal to 0, the price is displayed in dollars; if it is equal to 1, it is displayed in euros.

CD_Formatting_EuroDollar_Ex

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

// The cultures used to format the values for the two different currencies.
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;
    if (e.Column.FieldName == "Price" && e.ListSourceRowIndex != DevExpress.XtraGrid.GridControl.InvalidRowHandle)
    {
        int currencyType = (int)view.GetListSourceRowCellValue(e.ListSourceRowIndex, "CurrencyType");
        decimal price = Convert.ToDecimal(e.Value);
        switch (currencyType)
        {
            case 0: e.DisplayText = string.Format(ciUSA, "{0:c}", price); break;
            case 1: e.DisplayText = string.Format(ciEUR, "{0:c}", price); break;
        }
    }
}