• 更改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

  • 相关阅读:
    Python编程知识
    Ubuntu 20.04.3 LTS + Intel Realsense 400系列
    Kubectl
    在Debian Buster中安装redis-cli
    MySQL中最近执行的查询的信息
    EntityFramework 在脚本中使用in
    CLDR TimeZone Mapper
    Skyspark Axon
    Async Restsharp call
    HTTP 1.1 中的Accept-Language header
  • 原文地址:https://www.cnblogs.com/songxingzhu/p/4205694.html
Copyright © 2020-2023  润新知