在DevExpress网格控件中正确格式化货币

Kev*_*lia 0 c# devexpress currency winforms gridcontrol

所以我已经看到了其他问题,但我不能为我的生活让我的网格格式我的浮动作为货币.这是我的简单项目,它有一个名为gridcontrol1的网格控件,有4列,我希望最后一个是货币,另外3个是字符串.

public partial class Form1 : Form
{
    private DevExpress.XtraGrid.GridControl gridControl1;
    private DevExpress.XtraGrid.Views.Grid.GridView gridView1;
    private DevExpress.XtraGrid.Columns.GridColumn gridColumn1;
    private DevExpress.XtraGrid.Columns.GridColumn gridColumn2;
    private DevExpress.XtraGrid.Columns.GridColumn gridColumn3;
    private DevExpress.XtraGrid.Columns.GridColumn gridColumn4;

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        ArrayList test = new ArrayList();
        test.Add(new MyObject() { myCurrency = 1.5F, prop1 = "hi", prop2 = "hi2", prop3 = "hi3" });

        gridColumn4.DisplayFormat.FormatString = "c";
        gridColumn4.DisplayFormat.FormatType = DevExpress.Utils.FormatType.Custom;

        gridControl1.DataSource = test;
        gridControl1.MainView.PopulateColumns();
        gridControl1.RefreshDataSource();
    }
}

public class MyObject
{
    public string prop1 { get; set; }
    public string prop2 { get; set; }
    public string prop3 { get; set; }
    public float myCurrency { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

我尝试了自定义和数字的'c','c2','N','N2'和FormatType的格式字符串及其任何组合,并在框中列出了获得"1.5"的相同结果.我做了一些简单的错误吗?这不可能那么难!

Dmi*_*ryG 6

请尝试以下(这对我来说很好):

GridColumn colCurrency = gridView1.Columns["myCurrency"];
colCurrency.DisplayFormat.FormatType = DevExpress.Utils.FormatType.Custom;
colCurrency.DisplayFormat.FormatString = "c";
Run Code Online (Sandbox Code Playgroud)

相关链接:GridColumn.DisplayFormat属性

同时删除该ColumnView.PopulateColumns命令,因为在调用此方法时会清除GridView.Columns集合.因此,您为设计器中的列设置的显示格式不会影响新创建的列.