DevExpress实现自定义GridControl中按钮文字内容的方法
程序员文章站
2024-02-18 11:47:22
在一些c#项目的开发中,有时候会使用到gridcontrol的findpanel,这样可以很好的对数据进行筛选,可是所展现的按钮文字是英文,如下图所示:
那怎么定义两...
在一些c#项目的开发中,有时候会使用到gridcontrol的findpanel,这样可以很好的对数据进行筛选,可是所展现的按钮文字是英文,如下图所示:
那怎么定义两个按钮问题,以符合项目需求了?本文讲述利用gridlocalizer来实现这一要求的方法。具体步骤如下:
主要功能代码如下:
public class buildergridlocalizer : gridlocalizer { dictionary<gridstringid, string> cuslocalizedkeyvalue = null; /// <summary> /// 带参数的构造函数 /// </summary> /// <param name="cuslocalizedkeyvalue">需要转移的gridstringid,其对应的文字描述</param> public buildergridlocalizer(dictionary<gridstringid, string> cuslocalizedkeyvalue) { cuslocalizedkeyvalue = cuslocalizedkeyvalue; } /// <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(); }
代码运行效果如下图所示: