【问题标题】:How to format column to show percent (%) in the xtragrid如何格式化列以在 xtragrid 中显示百分比 (%)
【发布时间】:2012-05-21 09:18:35
【问题描述】:

我有一个 xtragrid,其中包含来自存储过程的值。
我得到浮点数 (0.23) 的值,我想以百分比 (23%) 显示。
在 C# 中最好的方法是什么?

之前 后

【问题讨论】:

    标签: devexpress xtragrid


    【解决方案1】:

    如果您只想以只读方式显示单元格,请使用 CustomDrawCell 事件。或者您也可以使用CustomColumnDisplayText Event

    试试这个:

    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() + "%";
                    }
                }
    

    另一种方法是使用Editor EditFormat 和DisplayFormat 属性。并在设置这些属性后将其添加到 gridview 列:参考: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;
    
            }
    

    最简单的方法是使用GridColumn.DisplayFormat 属性。

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

    希望对您有所帮助..

    【讨论】:

    • 这正是我想要的。所有参考文献 +1。
    • 很好,这也让我可以将百分比存储为字节/小整数 - 谢谢!否则 GridControl 没有按照我的意愿解释数据 -> 10% 变成 1000% :)
    • @Aron:这里有什么问题??如果有问题,请提出更改建议,以使答案更好并对其他人有所帮助。
    【解决方案2】:

    请使用以下方法:

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

    相关链接:

    【讨论】:

    • capital P 将其视为50 as 50%,如果你想使用比例0 to 1,则必须在格式字符串中使用小p..
    • @NiranjanKala:谢谢。这是我的错。我已经更新了我的答案。
    猜你喜欢
    • 2013-08-26
    • 2013-10-25
    • 1970-01-01
    • 2019-07-03
    • 1970-01-01
    • 1970-01-01
    • 2016-03-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多