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

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

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

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

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

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

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

方法如下:

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

其次,建立扩展类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Grid; namespace DXApplication3
{
public static class GridViewMasterExtenstion
{
static Dictionary<int, IEnumerable<string>> _relationNames;
static Dictionary<int, Action<GridView, MasterRowGetRelationNameEventArgs>> _displayCaptions;
static Dictionary<int, Action<GridView, MasterRowGetChildListEventArgs>> _childLists; static GridViewMasterDetailExtenstion()
{
_relationNames = new Dictionary<int, IEnumerable<string>>();
_displayCaptions = new Dictionary<int, Action<GridView, MasterRowGetRelationNameEventArgs>>();
_childLists = new Dictionary<int, Action<GridView, MasterRowGetChildListEventArgs>>();
} public static void MasterDetails(this GridView gridView,
string relationName,
Action<GridView, MasterRowGetChildListEventArgs> childLists)
{
MasterDetails(gridView, new[] { relationName }, null, childLists);
} public static void MasterDetails(this GridView gridView,
IEnumerable<string> relationNames,
Action<GridView, MasterRowGetChildListEventArgs> childLists)
{
MasterDetails(gridView, relationNames, null, childLists);
} public static void MasterDetails(this GridView gridView,
IEnumerable<string> relationNames,
Action<GridView, MasterRowGetRelationNameEventArgs> displayCaptions,
Action<GridView, MasterRowGetChildListEventArgs> childLists)
{
if (relationNames == null)
{
throw new ArgumentNullException("relationNames can not be null.");
} _relationNames[gridView.GetHashCode()] = relationNames; if (displayCaptions != null)
_displayCaptions[gridView.GetHashCode()] = displayCaptions; _childLists[gridView.GetHashCode()] = childLists; gridView.MasterRowEmpty += GridView_MasterRowEmpty;
gridView.MasterRowGetChildList += GridView_MasterRowGetChildList;
gridView.MasterRowGetRelationName += GridView_MasterRowGetRelationName;
gridView.MasterRowGetRelationCount += GridView_MasterRowGetRelationCount;
gridView.MasterRowGetRelationDisplayCaption += GridView_MasterRowGetRelationDisplayCaption;
} private static void GridView_MasterRowEmpty(object sender, MasterRowEmptyEventArgs e)
{
if (e.RowHandle == GridControl.InvalidRowHandle)
return; e.IsEmpty = false;
} private static void GridView_MasterRowGetChildList(object sender, MasterRowGetChildListEventArgs e)
{
if (e.RowHandle == GridControl.InvalidRowHandle)
return; var key = GetGridViewHashCode(sender as GridView);
if (_childLists.ContainsKey(key))
{
var childList = _childLists[key];
childList(sender as GridView, e);
}
} private static void GridView_MasterRowGetRelationCount(object sender, MasterRowGetRelationCountEventArgs e)
{ var key = GetGridViewHashCode(sender as GridView);
if (_relationNames.ContainsKey(key))
e.RelationCount = _relationNames[key].Count();
else
e.RelationCount = ;
} private static void GridView_MasterRowGetRelationName(object sender, MasterRowGetRelationNameEventArgs e)
{
if (e.RowHandle == GridControl.InvalidRowHandle)
return; var key = GetGridViewHashCode(sender as GridView);
if (_relationNames.ContainsKey(key))
{
e.RelationName = _relationNames[key].Skip(e.RelationIndex).FirstOrDefault();
}
} private static void GridView_MasterRowGetRelationDisplayCaption(object sender, MasterRowGetRelationNameEventArgs e)
{
if (e.RowHandle == GridControl.InvalidRowHandle)
return; var key = GetGridViewHashCode(sender as GridView);
if (_displayCaptions.ContainsKey(key))
{
var displayCaptions = _displayCaptions[key];
displayCaptions(sender as GridView, e);
} } private static int GetGridViewHashCode(GridView gridView)
{
if (gridView.SourceView == null)
return gridView.GetHashCode();
else
return gridView.SourceView.GetHashCode();
}
}
}

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

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms; namespace DXApplication3
{
public partial class Form1 : DevExpress.XtraEditors.XtraForm
{
public Form1()
{
InitializeComponent();
this.CreateList();
gridControl1.DataSource = lstPerson;
//调用模式如下,“Level1”对应Level的名称
gridView1.MasterDetails("Level1", (gridView2, a) =>
{
Person item = gridView1.GetRow(gridView1.FocusedRowHandle) as Person;
List<Company> lstCompany = new List<Company>();
for (int i = ; i < ; i++)
{
Company com = new Company();
com.Name = "A" + i;
com.Sale = (i * ).ToString();
lstCompany.Add(com);
}
a.ChildList = lstCompany;
});
gridControl1.RefreshDataSource();
} List<Person> lstPerson = new List<Person>();
public void CreateList() {
Person p1 = new Person();
p1.Name = "A";
p1.Address = "中国";
p1.Age = "";
Person p2 = new Person();
p2.Name = "B";
p2.Address = "美国";
p2.Age = "";
lstPerson.Add(p1);
lstPerson.Add(p2);
}
}
public class Person
{
public string Name
{
set;
get;
}
public string Age
{
set;
get;
}
public string Address
{
set;
get;
}
}
public class Company
{
public string Name
{
set;
get;
}
public string Sale
{
set;
get;
}
}
}

用扩展方法实现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. 常用的linux系统监控命令整理

    找到最耗CPU的java线程ps命令 命令:ps -mp pid -o THREAD,tid,time 或者 ps -Lfp pid 结果展示: 这个命令的作用,主要是可以获取到对应一个进程下的线程的 ...

  2. c专家编程---优先级规则

    对于一些复杂的类型组合,总是搞不明白,今天阅读了“优先级规则”这块,有了进一步的理解,特将规则记在此处,供自己学习查询使用. 优先级规则: A.声明从它的名字开始读取,然后按照优先级顺序依次读取 B. ...

  3. Inno Setup入门(八)——有选择性的安装文件

    这主要使用[Components]段实现,一个演示的代码如下: [setup] ;全局设置,本段必须 AppName=Test AppVerName=TEST DefaultDirName=" ...

  4. kafuka常用的shell命令

    kafka常用shell命令: ------------------------------------ 1.创建topic bin/kafka-topics.sh --create --zookee ...

  5. ListView控件的Insert、Edit和Delete功能(第一部分)

    摘自:http://blog.ashchan.com/archive/2007/08/28/listview-control-insert-edit-amp-delete-part-1aspx/ Li ...

  6. MyBatis 返回新增数据的自增id

    <insert id="save" parameterType="Vote" useGeneratedKeys="true" keyP ...

  7. Linux下的编程实战【转】

    一篇比较不错的文章, 降到了 makefile make , gcc编译器,GDB调试器, Linux文件系统,Linux文件API,.C语言库函数(C库函数的文件操作实际上是独立于具体的操作系统平台 ...

  8. Notification使用笔记

    之前在项目中使用了Notification,现分享出来: checkNotification() function checkNotification(){ //判断是否支持Notification ...

  9. OpenGL中glFrustum()和gluPerspective()的相互转换

    OpenGL中在窗口的大小发生变化的时候会触发resize()函数,这里会传入一个新的宽和高,在resize()函数中我们会设置投影矩阵,在可以使用OpenGL基础函数glFrustum()函数和gl ...

  10. cout、cerr、clog

    其实大家平常常会用的主要有三个:cout.cerr.clog,首先简单介绍下三者. 这三者在C++中都是标准IO库中提供的输出工具(至于有关的重载问题在此不讨论): cout:写到标准输出的ostre ...