我有一个包含存储过程值的xtragrid 我得到浮点值(0.23),我想以百分比(23%)显示 在C#中最好的方法是什么?
前
后
答案 0 :(得分:10)
如果您只想以只读方式显示单元格,请使用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列: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;
}
最简单的方法是使用GridColumn.DisplayFormat属性。
colPayment.DisplayFormat.FormatType = FormatType.Numeric;
colPayment.DisplayFormat.FormatString = "p0";
希望这有帮助..
答案 1 :(得分:9)
请使用以下方法:
colReabilitation.DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
colReabilitation.DisplayFormat.FormatString = "p0";
相关链接: