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

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

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

 public partial class Form1 : XtraForm
    {
        public Form1()
        {
            InitializeComponent();
            InitGrid();
            gridView1.Columns[2].DisplayFormat.FormatType = DevExpress.Utils.FormatType.Custom;
            gridView1.Columns[2].DisplayFormat.Format = new MyExamFormat();
        }

        public class MyExamFormat : IFormatProvider, ICustomFormatter
        {
            public string NumberToChinese(int num)
            {
                string numStr = "0123456789";
                string chineseStr = "零一二三四五六七八九";
                if (num >= 0 && num <= 9) { return chineseStr[numStr.IndexOf(num.ToString()[0])].ToString(); }
                else if (num >= 10 && num <= 19) { if (num == 10) { return "十"; } return "十" + chineseStr[numStr.IndexOf(num.ToString()[1])].ToString(); }
                else if (num >= 20 && num <= 99)
                {
                    if (num % 10 == 0) { return chineseStr[numStr.IndexOf(num.ToString()[0])].ToString() + "十"; }
                    else return chineseStr[numStr.IndexOf(num.ToString()[0])].ToString() + "十" + chineseStr[numStr.IndexOf(num.ToString()[1])].ToString();
                }
                else if (num >= 100 && num <= 999)
                {
                    if (num % 100 == 0) { return chineseStr[numStr.IndexOf(num.ToString()[0])].ToString() + "百"; }
                    else return chineseStr[numStr.IndexOf(num.ToString()[0])].ToString() + "百" +
                      ((int.Parse(num.ToString().Substring(1).ToString()).ToString().Length != (num.ToString().Length - 1)) ?
                         "零" + NumberToChinese(int.Parse(num.ToString().Substring(1)))
                        : NumberToChinese(int.Parse(num.ToString().Substring(1))));
                }
                else if (num >= 1000 && num <= 9999)
                {
                    if (num % 1000 == 0) { return chineseStr[numStr.IndexOf(num.ToString()[0])].ToString() + "千"; }
                    else return
                         chineseStr[numStr.IndexOf(num.ToString()[0])].ToString() + "千" +
                      ((int.Parse(num.ToString().Substring(1).ToString()).ToString().Length != (num.ToString().Length - 1)) ?
                         "零" + NumberToChinese(int.Parse(num.ToString().Substring(1)))
                        : NumberToChinese(int.Parse(num.ToString().Substring(1))));
                }
                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", 10));
            gridDataList.Add(new Person("Gabriel", "Smith", 15));
            gridDataList.Add(new Person("Ashley", "Smithfdsaf", 11));
            gridDataList.Add(new Person("Adrian", "some comment", 987));
            gridControl.DataSource = gridDataList;
        }

    }

显示效果如下:

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


更改DEVExpress的Column的DisplayFormat为自定义的方法。
https://www.dearcloud.cn/2015/01/06/20200310-cnblogs-old-posts/20150106-更改DEVExpress的Column的DisplayFormat为自定义的方法/
作者
宋兴柱
发布于
2015年1月6日
许可协议