首先,让我们先回顾下.Net中扩展方法的特征:

1、  必须在一个非嵌套、非泛型的静态类中;

2、  至少有一个参数(this 作前缀的参数);

3、  第一个参数必须附加this做前缀;

4、  第一个参数不能加任何修饰符(如out或ref);

5、  第一个参数的类型不能是指针类型。

在DevExpress控件中GridControl是非常常用的控件之一,有时后为更好的展示效果而使用级联功能。在通常情况下为实现这种功能需要在一个实体类中建立令一个实体类的集合(该集合以备级联下的GridView使用),这样实行增加类实体类的复杂程度,且在项目比较大的情况下这种模式更降低了程序的可读性与可维护性,在此写了GridControl的扩展类,使整个项目都可以按照统一的模式建立GridControl的级联效果,且简化了实体类。

方法如下:

首先,建立GridControl,并对GridView的列绑定好相应的字段名称,在这就不细说啦。

其次,建立扩展类:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using DevExpress.XtraGrid;
  7. using DevExpress.XtraGrid.Views.Grid;
  8.  
  9. namespace DXApplication3
  10. {
  11. public static class GridViewMasterExtenstion
  12. {
  13. static Dictionary<int, IEnumerable<string>> _relationNames;
  14. static Dictionary<int, Action<GridView, MasterRowGetRelationNameEventArgs>> _displayCaptions;
  15. static Dictionary<int, Action<GridView, MasterRowGetChildListEventArgs>> _childLists;
  16.  
  17. static GridViewMasterDetailExtenstion()
  18. {
  19. _relationNames = new Dictionary<int, IEnumerable<string>>();
  20. _displayCaptions = new Dictionary<int, Action<GridView, MasterRowGetRelationNameEventArgs>>();
  21. _childLists = new Dictionary<int, Action<GridView, MasterRowGetChildListEventArgs>>();
  22. }
  23.  
  24. public static void MasterDetails(this GridView gridView,
  25. string relationName,
  26. Action<GridView, MasterRowGetChildListEventArgs> childLists)
  27. {
  28. MasterDetails(gridView, new[] { relationName }, null, childLists);
  29. }
  30.  
  31. public static void MasterDetails(this GridView gridView,
  32. IEnumerable<string> relationNames,
  33. Action<GridView, MasterRowGetChildListEventArgs> childLists)
  34. {
  35. MasterDetails(gridView, relationNames, null, childLists);
  36. }
  37.  
  38. public static void MasterDetails(this GridView gridView,
  39. IEnumerable<string> relationNames,
  40. Action<GridView, MasterRowGetRelationNameEventArgs> displayCaptions,
  41. Action<GridView, MasterRowGetChildListEventArgs> childLists)
  42. {
  43. if (relationNames == null)
  44. {
  45. throw new ArgumentNullException("relationNames can not be null.");
  46. }
  47.  
  48. _relationNames[gridView.GetHashCode()] = relationNames;
  49.  
  50. if (displayCaptions != null)
  51. _displayCaptions[gridView.GetHashCode()] = displayCaptions;
  52.  
  53. _childLists[gridView.GetHashCode()] = childLists;
  54.  
  55. gridView.MasterRowEmpty += GridView_MasterRowEmpty;
  56. gridView.MasterRowGetChildList += GridView_MasterRowGetChildList;
  57. gridView.MasterRowGetRelationName += GridView_MasterRowGetRelationName;
  58. gridView.MasterRowGetRelationCount += GridView_MasterRowGetRelationCount;
  59. gridView.MasterRowGetRelationDisplayCaption += GridView_MasterRowGetRelationDisplayCaption;
  60. }
  61.  
  62. private static void GridView_MasterRowEmpty(object sender, MasterRowEmptyEventArgs e)
  63. {
  64. if (e.RowHandle == GridControl.InvalidRowHandle)
  65. return;
  66.  
  67. e.IsEmpty = false;
  68. }
  69.  
  70. private static void GridView_MasterRowGetChildList(object sender, MasterRowGetChildListEventArgs e)
  71. {
  72. if (e.RowHandle == GridControl.InvalidRowHandle)
  73. return;
  74.  
  75. var key = GetGridViewHashCode(sender as GridView);
  76. if (_childLists.ContainsKey(key))
  77. {
  78. var childList = _childLists[key];
  79. childList(sender as GridView, e);
  80. }
  81. }
  82.  
  83. private static void GridView_MasterRowGetRelationCount(object sender, MasterRowGetRelationCountEventArgs e)
  84. {
  85.  
  86. var key = GetGridViewHashCode(sender as GridView);
  87. if (_relationNames.ContainsKey(key))
  88. e.RelationCount = _relationNames[key].Count();
  89. else
  90. e.RelationCount = ;
  91. }
  92.  
  93. private static void GridView_MasterRowGetRelationName(object sender, MasterRowGetRelationNameEventArgs e)
  94. {
  95. if (e.RowHandle == GridControl.InvalidRowHandle)
  96. return;
  97.  
  98. var key = GetGridViewHashCode(sender as GridView);
  99. if (_relationNames.ContainsKey(key))
  100. {
  101. e.RelationName = _relationNames[key].Skip(e.RelationIndex).FirstOrDefault();
  102. }
  103. }
  104.  
  105. private static void GridView_MasterRowGetRelationDisplayCaption(object sender, MasterRowGetRelationNameEventArgs e)
  106. {
  107. if (e.RowHandle == GridControl.InvalidRowHandle)
  108. return;
  109.  
  110. var key = GetGridViewHashCode(sender as GridView);
  111. if (_displayCaptions.ContainsKey(key))
  112. {
  113. var displayCaptions = _displayCaptions[key];
  114. displayCaptions(sender as GridView, e);
  115. }
  116.  
  117. }
  118.  
  119. private static int GetGridViewHashCode(GridView gridView)
  120. {
  121. if (gridView.SourceView == null)
  122. return gridView.GetHashCode();
  123. else
  124. return gridView.SourceView.GetHashCode();
  125. }
  126. }
  127. }

最后,举例,在程序中调用:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9.  
  10. namespace DXApplication3
  11. {
  12. public partial class Form1 : DevExpress.XtraEditors.XtraForm
  13. {
  14. public Form1()
  15. {
  16. InitializeComponent();
  17. this.CreateList();
  18. gridControl1.DataSource = lstPerson;
  19. //调用模式如下,“Level1”对应Level的名称
  20. gridView1.MasterDetails("Level1", (gridView2, a) =>
  21. {
  22. Person item = gridView1.GetRow(gridView1.FocusedRowHandle) as Person;
  23. List<Company> lstCompany = new List<Company>();
  24. for (int i = ; i < ; i++)
  25. {
  26. Company com = new Company();
  27. com.Name = "A" + i;
  28. com.Sale = (i * ).ToString();
  29. lstCompany.Add(com);
  30. }
  31. a.ChildList = lstCompany;
  32. });
  33. gridControl1.RefreshDataSource();
  34. }
  35.  
  36. List<Person> lstPerson = new List<Person>();
  37. public void CreateList() {
  38. Person p1 = new Person();
  39. p1.Name = "A";
  40. p1.Address = "中国";
  41. p1.Age = "";
  42. Person p2 = new Person();
  43. p2.Name = "B";
  44. p2.Address = "美国";
  45. p2.Age = "";
  46. lstPerson.Add(p1);
  47. lstPerson.Add(p2);
  48. }
  49. }
  50. public class Person
  51. {
  52. public string Name
  53. {
  54. set;
  55. get;
  56. }
  57. public string Age
  58. {
  59. set;
  60. get;
  61. }
  62. public string Address
  63. {
  64. set;
  65. get;
  66. }
  67. }
  68. public class Company
  69. {
  70. public string Name
  71. {
  72. set;
  73. get;
  74. }
  75. public string Sale
  76. {
  77. set;
  78. get;
  79. }
  80. }
  81. }

用扩展方法实现DevExpress-GridControl级联效果的更多相关文章

  1. 扩展方法实现DevExpress控件校验

    DevExpress控件中,如果要控件的值进行校验,需要用到DXValidationProvider控件和DXErrorProvider控件,按照正常思路,无论使用哪个控件要实现校验效果时都需要对每个 ...

  2. 【细语】C#之扩展方法原理及其使用

    1.写在前面 今天群里一个小伙伴问了这样一个问题,扩展方法与实例方法的执行顺序是什么样子的,谁先谁后(这个问题会在文章结尾回答).所以写了这边文章,力图从原理角度解释扩展方法及其使用. 以下为主要内容 ...

  3. DevExpress GridControl使用方法

    一.如何解决单击记录整行选中的问题 View->OptionsBehavior->EditorShowMode 设置为:Click 二.如何新增一条记录 (1).gridView.AddN ...

  4. DevExpress GridControl 使用方法技巧 总结 收录整理

    一.如何解决单击记录整行选中的问题 View->OptionsBehavior->EditorShowMode 设置为:Click 二.如何新增一条记录 ().gridView.AddNe ...

  5. C# DevExpress GridControl使用方法

    一.如何解决单击记录整行选中的问题 View->OptionsBehavior->EditorShowMode 设置为:Click 二.如何新增一条记录 ().gridView.AddNe ...

  6. [转载]DevExpress GridControl 使用方法技巧 总结 收录整理

    最近开始用DevExpress组件,发现很好的经验总结博客,在这里转载分享 原作者:https://www.cnblogs.com/wordgao/p/4517011.html 一.如何解决单击记录整 ...

  7. DevExpress GridControl使用(转)

    DevExpress GridControl使用 (一)原汁原味的表格展示 Dev控件中的表格控件GridControl控件非常强大.不过,一些细枝末节的地方有时候用起来不好找挺讨厌的.使用过程中,多 ...

  8. Devexpress GridControl z

    http://minmin86121.blog.163.com/blog/static/4968115720144194923578/ 1 AllowNullInput=False; --Devexp ...

  9. DevExpress GridControl 单元格添加进度条(ProgressBar)

    首先可以使用DevExpress GridControl 自带的进度条控件. 但是我要用一个方法来设置所以的单元格进度,而不是每个单元格都要设置一遍,同时我想要根据进度值不同,进度条显示不同的颜色. ...

随机推荐

  1. hdu_5707_Combine String("巴卡斯杯" 中国大学生程序设计竞赛 - 女生专场)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=5707 题意:给你三个字符串 a,b,c,问你 c能否拆成a,b,a,b串的每一个字符在c中不能变 题解 ...

  2. Git 操作标签的一些命令

    如果标签打错了,也是可以删除: $ git tag -d v0.1Deleted tag 'v0.1' (was d96a49b) 如果要推送某个标签到远程,使用git push orign tagn ...

  3. iOS应用程序内存查看工具

    我要找的是一个可以检查应用程序中哪一个数组存贮的什么内容的工具. 网上搜到的工具名称是Allocations Instrument,后来一试发现不是我想要的.这还是一个后期调试阶段的内存检查工具. h ...

  4. tcpdump抓取HTTP包【转载】

    tcpdump -XvvennSs 0 -i eth0 tcp[20:2]=0x4745 or tcp[20:2]=0x4854 0x4745 为"GET"前两个字母"G ...

  5. Moya 浅析

    Moya是一个高度抽象的网络库,他的理念是让你不用关心网络请求的底层的实现细节,只用定义你关心的业务.且Moya采用桥接和组合来进行封装(默认桥接了Alamofire),使得Moya非常好扩展,让你不 ...

  6. UIImage+Scale

    Scale a UIImage to any given rect keeping the aspect ratio Raw  UIImage+Scale.m   @implementation UI ...

  7. div里面的margin-top失效

    div标签中的元素margin-top失效的解决方法 元素上级标签是div,已经设置了width和height等的属性,可是,在对元素使用margin进行调整的时候,无法生效,下面有个不错的解决方法, ...

  8. java 多线程机制

    Example12_1.java public class Example12_1 { public static void main(String args[]) { //主线程 SpeakElep ...

  9. 2--OC -- 类的创建与实例化

    2.OC -- 类的创建与实例化   一.OC类的简述 1.OC类分为2个文件:.h文件用于类的声明,.m文件用于实现.h的函数: 2.类是声明使用关键字:@interface.@end : 3.类是 ...

  10. k-Means和KNN算法简述

    k-means 算法 k-means 算法接受输入量 k :然后将n个数据对象划分为 k个聚类以便使得所获得的聚类满足:同一聚类中的对象相似度较高:而不同聚类中的对象相似度较小.聚类相似度是利用各聚类 ...