這兩天用VGridControl,有個界面是根據參數需要動態生成行和對應的編輯控件。我想根據參數中的單位來格式化顯示效果,如把5顯示成5.00 KG。
在Runtime中寫入如下語句:
editor.DisplayFormat.FormatType = DevExpress.Utils.FormatType.Custom;
editor.DisplayFormat.FormatString = "0.00 " + property.Unit;
但是卻無法得到想要的效果,查看了文檔后之后才發現,原來綁定的源數據中的DataTable字段都被我設成了string類型,必須要改成對於的字段類型才可以
DataColumn col = new DataColumn();
col.ColumnName = property.Id.ToString();
if (property.DataType == (int)DataType.Int)
col.DataType = typeof(int);
else if (property.DataType == (int)DataType.Float)
col.DataType = typeof(float);
else if (property.DataType == (int)DataType.Datetime)
col.DataType = typeof(DateTime);
else if (property.DataType == (int)DataType.Bool)
col.DataType = typeof(bool);
else
col.DataType = typeof(string);
dtProperty.Columns.Add(col);
這樣就顯示正確了