簡體   English   中英

在DevExpress網格控件中正確設置貨幣格式

[英]Format currency correctly in DevExpress Grid Control

因此,我還看到了其他問題,但是我一生無法將網格格式的浮點數設置為貨幣。 這是我的簡單項目,它有一個名為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; }
}

我試過了自定義和數字的'c','c2','N','N2'和FormatType格式字符串,以及它們的任意組合,其結果相同,在框中列出了'1.5'。 我是在做簡單的錯誤事情嗎? 可以這么難!

請嘗試以下操作(對我來說效果很好):

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

相關鏈接: GridColumn.DisplayFormat屬性

還要刪除ColumnView.PopulateColumns命令,因為調用此方法時會清除GridView.Columns集合。 因此,您在設計器中為列設置的顯示格式不會影響新創建的列。

代替

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

只需向上移動第二行:

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