如何在DevExpress Winform中更改加载时的单元格格式

时间:2017-06-23 08:56:16

标签: winforms devexpress

我想在GridView中更改单元格格式,单元格值,单元格加载时的对齐方式。

1 个答案:

答案 0 :(得分:0)

请查看此链接https://documentation.devexpress.com/windowsforms/3048/Controls-and-Libraries/Data-Grid/Examples/Formatting/How-to-Format-Display-Text-in-Grid-s-Cells-Depending-on-Values-in-Other-Cells

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