简体   繁体   English

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

[英]Format currency correctly in DevExpress Grid Control

So I've seen other questions on this, but I can't for the life of me make my grid format my float as currency. 因此,我还看到了其他问题,但是我一生无法将网格格式的浮点数设置为货币。 Here's my simple project, it has a grid control called gridcontrol1 with 4 columns, I want the last one to be currency, the other 3 to be string. 这是我的简单项目,它有一个名为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; }
}

I have tried format string of 'c', 'c2', 'N', 'N2' and FormatType of both custom and numeric and any combination thereof with the same result of getting '1.5' listed in the box. 我试过了自定义和数字的'c','c2','N','N2'和FormatType格式字符串,以及它们的任意组合,其结果相同,在框中列出了'1.5'。 Am I doing something simple wrong? 我是在做简单的错误事情吗? This can't be that hard! 可以这么难!

Please, try the following (this works fine to me): 请尝试以下操作(对我来说效果很好):

GridColumn colCurrency = gridView1.Columns["myCurrency"];
colCurrency.DisplayFormat.FormatType = DevExpress.Utils.FormatType.Custom;
colCurrency.DisplayFormat.FormatString = "c";

Related link: GridColumn.DisplayFormat Property 相关链接: GridColumn.DisplayFormat属性

Also remove the ColumnView.PopulateColumns command as GridView.Columns collection is cleared when this method is called. 还要删除ColumnView.PopulateColumns命令,因为调用此方法时会清除GridView.Columns集合。 So, the Display format you set for columns in the designer does not affect the newly created column. 因此,您在设计器中为列设置的显示格式不会影响新创建的列。

Instead of 代替

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

Just move up the second line: 只需向上移动第二行:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM