8

I have a xtragrid with values from a stored procedure.
I am getting the values in float (0.23) and I want to display in percent (23%).
What would be the best way to do it in C#?

before before after after

Stavros
  • 5,802
  • 13
  • 32
  • 45

2 Answers2

10

Use CustomDrawCell event if you just want to display the cell readonly. or You can use the CustomColumnDisplayText Event also.

Try this:

private void gridView1_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
            {
                if (e.Column.FieldName == "Marks")
                {
                    e.DisplayText = (((int)(Convert.ToDecimal(e.CellValue) * 100))).ToString() + "%";
                }
            }

Another way use the Editor EditFormat and DisplayFormat property. and after setting these properties add it to the gridview column: Ref: How to format values shown in the XtraGrid

RepositoryItem textEdit;
        private void AddGridRepositoryItem()
        {
            textEdit = new RepositoryItemTextEdit();
            textEdit.AllowHtmlDraw = DevExpress.Utils.DefaultBoolean.True;
            textEdit.ReadOnly = true;
            gridControl1.RepositoryItems.Add(textEdit);

            textEdit.DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
            textEdit.DisplayFormat.FormatString = "p";
            gridView1.Columns[0].ColumnEdit = textEdit;

        }

The simplest way is use GridColumn.DisplayFormat Property.

colPayment.DisplayFormat.FormatType = FormatType.Numeric;
colPayment.DisplayFormat.FormatString = "p0";

Hope this help..

Niranjan Singh
  • 18,017
  • 2
  • 42
  • 75
  • 1
    That's exactly what I was looking for. +1 for all the references. – Stavros May 21 '12 at 10:59
  • Nice, and this also allowed me to store percents as byte/tinyint - thanks! Otherwise GridControl was not interpreting data as I wanted -> 10 percents turned into 1000% :) – Prokurors Mar 11 '14 at 17:31
  • @Aron: What is wrong here?? If something wrong then suggest the changes to make the answers better and helpful to others. – Niranjan Singh Sep 30 '16 at 09:27
9

Please use the following approach:

colReabilitation.DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
colReabilitation.DisplayFormat.FormatString = "p0";

Related links:

DmitryG
  • 17,677
  • 1
  • 30
  • 53