首页 > 代码库 > [WinForm][DevExpress]自定义GridControl中按钮文字内容

[WinForm][DevExpress]自定义GridControl中按钮文字内容

最近项目开发中,使用到了GridControl的FindPanel,这样可以很好的对数据进行筛选,可是所展现的按钮文字是英文,如图:

image

那怎么定义两个按钮问题,以符合项目需求了?经过一番搜索发现利用GridLocalizer可以很好实现:

核心代码:

   public class BuilderGridLocalizer : GridLocalizer    {        Dictionary<GridStringId, string> CusLocalizedKeyValue = http://www.mamicode.com/null;        /// <summary>        /// 带参数的构造函数        /// </summary>        /// <param name="cusLocalizedKeyValue">需要转移的GridStringId,其对应的文字描述</param>        public BuilderGridLocalizer(Dictionary<GridStringId, string> cusLocalizedKeyValue)        {            CusLocalizedKeyValue = http://www.mamicode.com/cusLocalizedKeyValue;"rem">/// <summary>        /// 重载        /// </summary>        /// <param name="id">GridStringId</param>        /// <returns>string</returns>        public override string GetLocalizedString(GridStringId id)        {            if (CusLocalizedKeyValue != null)            {                string _gridStringDisplay = string.Empty;                foreach (KeyValuePair<GridStringId, string> gridLocalizer in CusLocalizedKeyValue)                {                    if (gridLocalizer.Key.Equals(id))                    {                        _gridStringDisplay = gridLocalizer.Value;                        break;                    }                }                return _gridStringDisplay;            }            return base.GetLocalizedString(id);        }    }
定义一个类继承GridLocalizer,重写GetLocalizedString方法,逻辑很简单;为了以后能更简单重复利用代码,减少重复编码,所以在另外一个类中添加一个方法,如下:
        /// <summary>        ///  自定义GridControl按钮文字        /// </summary>        /// <param name="girdview">GridView</param>        /// <param name="cusLocalizedKeyValue">需要转移的GridStringId,其对应的文字描述</param>        public static void CustomButtonText(this GridView girdview, Dictionary<GridStringId, string> cusLocalizedKeyValue)        {            BuilderGridLocalizer _bGridLocalizer = new BuilderGridLocalizer(cusLocalizedKeyValue);            GridLocalizer.Active = _bGridLocalizer;        }

这样子我们可以很简单使用,代码如下:

        private Dictionary<GridStringId, string> SetGridLocalizer()        {            Dictionary<GridStringId, string> _gridLocalizer = new Dictionary<GridStringId, string>();            _gridLocalizer.Add(GridStringId.FindControlFindButton, "查找");            _gridLocalizer.Add(GridStringId.FindControlClearButton, "清空");            return _gridLocalizer;        }        private void Form1_Load(object sender, EventArgs e)        {            Dictionary<GridStringId, string> _gridLocalizer = SetGridLocalizer();            gridView1.CustomButtonText(_gridLocalizer);            gridControl1.DataSource = CreateTable(20);            gridView1.ShowFindPanel();        }

<style type="text/css">.csharpcode, .csharpcode pre{ font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/}.csharpcode pre { margin: 0em; }.csharpcode .rem { color: #008000; }.csharpcode .kwrd { color: #0000ff; }.csharpcode .str { color: #006080; }.csharpcode .op { color: #0000c0; }.csharpcode .preproc { color: #cc6633; }.csharpcode .asp { background-color: #ffff00; }.csharpcode .html { color: #800000; }.csharpcode .attr { color: #ff0000; }.csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em;}.csharpcode .lnum { color: #606060; }</style>代码效果:

image

<style type="text/css">.csharpcode, .csharpcode pre{ font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/}.csharpcode pre { margin: 0em; }.csharpcode .rem { color: #008000; }.csharpcode .kwrd { color: #0000ff; }.csharpcode .str { color: #006080; }.csharpcode .op { color: #0000c0; }.csharpcode .preproc { color: #cc6633; }.csharpcode .asp { background-color: #ffff00; }.csharpcode .html { color: #800000; }.csharpcode .attr { color: #ff0000; }.csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em;}.csharpcode .lnum { color: #606060; }</style>