更改DEVExpress的Column的DisplayFormat为自定义的方法。

时间:2021-06-05 14:30:35

更改DEVExpress的Column的DisplayFormat为自定义的方法。

 public partial class Form1 : XtraForm
{
public Form1()
{
InitializeComponent();
InitGrid();
gridView1.Columns[].DisplayFormat.FormatType = DevExpress.Utils.FormatType.Custom;
gridView1.Columns[].DisplayFormat.Format = new MyExamFormat();
} public class MyExamFormat : IFormatProvider, ICustomFormatter
{
public string NumberToChinese(int num)
{
string numStr = "";
string chineseStr = "零一二三四五六七八九";
if (num >= && num <= ) { return chineseStr[numStr.IndexOf(num.ToString()[])].ToString(); }
else if (num >= && num <= ) { if (num == ) { return "十"; } return "十" + chineseStr[numStr.IndexOf(num.ToString()[])].ToString(); }
else if (num >= && num <= )
{
if (num % == ) { return chineseStr[numStr.IndexOf(num.ToString()[])].ToString() + "十"; }
else return chineseStr[numStr.IndexOf(num.ToString()[])].ToString() + "十" + chineseStr[numStr.IndexOf(num.ToString()[])].ToString();
}
else if (num >= && num <= )
{
if (num % == ) { return chineseStr[numStr.IndexOf(num.ToString()[])].ToString() + "百"; }
else return chineseStr[numStr.IndexOf(num.ToString()[])].ToString() + "百" +
((int.Parse(num.ToString().Substring().ToString()).ToString().Length != (num.ToString().Length - )) ?
"零" + NumberToChinese(int.Parse(num.ToString().Substring()))
: NumberToChinese(int.Parse(num.ToString().Substring())));
}
else if (num >= && num <= )
{
if (num % == ) { return chineseStr[numStr.IndexOf(num.ToString()[])].ToString() + "千"; }
else return
chineseStr[numStr.IndexOf(num.ToString()[])].ToString() + "千" +
((int.Parse(num.ToString().Substring().ToString()).ToString().Length != (num.ToString().Length - )) ?
"零" + NumberToChinese(int.Parse(num.ToString().Substring()))
: NumberToChinese(int.Parse(num.ToString().Substring())));
}
return num.ToString();
} public object GetFormat(Type formatType)
{
if (formatType == typeof(ICustomFormatter))
{
return this;
}
else return null;
} public string Format(string format, object arg, IFormatProvider formatProvider)
{
return "第" + NumberToChinese(int.Parse(arg + "")) + "题";
}
}
BindingList<Person> gridDataList = new BindingList<Person>();
void InitGrid()
{
gridDataList.Add(new Person("John", "Smith", ));
gridDataList.Add(new Person("Gabriel", "Smith", ));
gridDataList.Add(new Person("Ashley", "Smithfdsaf", ));
gridDataList.Add(new Person("Adrian", "some comment", ));
gridControl.DataSource = gridDataList;
} }

显示效果如下:

更改DEVExpress的Column的DisplayFormat为自定义的方法。

宋兴柱博客地址:http://www.cnblogs.com/songxingzhu