How to format column to show percent (%) in the xtragrid

Stavros picture Stavros · May 21, 2012 · Viewed 29.1k times · Source

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

Answer

Niranjan Singh picture Niranjan Singh · May 21, 2012

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..