zoukankan      html  css  js  c++  java
  • 更改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

  • 相关阅读:
    asp.net自己创建的app_code文件夹中的类不能访问的解决办法
    html+css实现选项卡功能 【转】
    jquery实现文本框数量加减功能的例子分享
    css3中的background
    通过CSS3 实现响应式Web设计的三个步骤
    鼠标移动到图片透明度改变
    jQuery之简单的表单验证【转】
    手机web——自适应网页设计(html/css控制)【转】
    css3 media媒体查询器用法总结
    把人团结在一起的力量
  • 原文地址:https://www.cnblogs.com/songxingzhu/p/4205694.html
Copyright © 2011-2022 走看看