VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > c#编程 >
  • C#教程之DevExpress实现自定义GridControl中按钮文字内

在一些C#项目的开发中,有时候会使用到GridControl的FindPanel,这样可以很好的对数据进行筛选,可是所展现的按钮文字是英文,如下图所示:

那怎么定义两个按钮问题,以符合项目需求了?本文讲述利用GridLocalizer来实现这一要求的方法。具体步骤如下:

主要功能代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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方法,逻辑很简单;为了以后能更简单重复利用代码,减少重复编码,所以在另外一个类中添加一个方法,如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/// <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();
}

代码运行效果如下图所示:

 

您可能感兴趣的文章:



相关教程