0

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.

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. Am I doing something simple wrong? This can't be that hard!

Kevin DiTraglia
  • 25,746
  • 19
  • 92
  • 138

2 Answers2

6

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

Also remove the ColumnView.PopulateColumns command as GridView.Columns collection is cleared when this method is called. So, the Display format you set for columns in the designer does not affect the newly created column.

Kevin DiTraglia
  • 25,746
  • 19
  • 92
  • 138
DmitryG
  • 17,677
  • 1
  • 30
  • 53
  • This isn't working for me, the field still displays without currency formatting. – Kevin DiTraglia Oct 02 '12 at 21:06
  • @KDiTraglia: It's strange... it should work as it described in documentation. It is difficult to determine the cause of this issue due to a lack of information. Thus I suggest you [report this issue to DevExpress Support Center](http://www.devexpress.com/Support/Center/Question/Create) (with the full sample attached). Anyway if you run into a problems with using DevExpress you should report these problems to DevExpress Support, instead of SO. – DmitryG Oct 03 '12 at 08:26
  • 1
    It turns out after asking dev express the reason it wasn't working was this line `gridControl1.MainView.PopulateColumns();` was resetting the layout, I'll leave you as the accepted answer since you lead me in the right direction but I'll edit in what actually solved this for me. – Kevin DiTraglia Oct 03 '12 at 14:10
0

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";
Peter B
  • 22,460
  • 5
  • 32
  • 69
Hikmet
  • 1
  • 1
  • I applied code formatting, you can do so yourself by selecting the lines of code in the edit box and then clicking the "**{}**" button (for every block of code). – Peter B Oct 19 '16 at 14:43