DevExpress-GridControl cascading effect achieved by extension method

First, let us first review under the extended method .Net features:

1, must be in a non-nested, non-generic static class;

2, at least one parameter (this parameter prefixed);

3, the first argument must do this additional prefix;

4, the first parameter can not add any modifiers (e.g., out or REF);

5, the first parameter can not be a pointer type.

 

In DevExpress controls GridControl is one very common controls, sometimes for the better after the display of results and cascading function. Under normal circumstances the need for this functionality is implemented in a set of entity class Order entity class (the set GridView used to prepare the cascade), so that implementation complexity increases based entity class, and the project is relatively large in the case of this model but also reduces the readability and maintainability of the program, I wrote in this category GridControl extended, so the whole project can create a cascade effect GridControl in accordance with the unified model, and simplified the entity classes.

Methods as below:

First, create GridControl, and binding good name of the corresponding field GridView column, in which it would not elaborate.

Secondly, the extension class:

Copy the code
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 = 0; } 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(); } } }
Copy the code

Finally, for example, called in the program:

Copy the code
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 = 0; i < 5; i++) { Company com = new Company(); com.Name = "A" + i; com.Sale = (i * 10).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 = "20"; Person p2 = new Person(); p2.Name = "B"; p2.Address = "美国"; p2.Age = "21"; 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; } } }
Copy the code

 

First, let us first review under the extended method .Net features:

1, must be in a non-nested, non-generic static class;

2, at least one parameter (this parameter prefixed);

3, the first argument must do this additional prefix;

4, the first parameter can not add any modifiers (e.g., out or REF);

5, the first parameter can not be a pointer type.

 

In DevExpress controls GridControl is one very common controls, sometimes for the better after the display of results and cascading function. Under normal circumstances the need for this functionality is implemented in a set of entity class Order entity class (the set GridView used to prepare the cascade), so that implementation complexity increases based entity class, and the project is relatively large in the case of this model but also reduces the readability and maintainability of the program, I wrote in this category GridControl extended, so the whole project can create a cascade effect GridControl in accordance with the unified model, and simplified the entity classes.

Methods as below:

First, create GridControl, and binding good name of the corresponding field GridView column, in which it would not elaborate.

Secondly, the extension class:

Copy the code
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 = 0; } 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(); } } }
Copy the code

Finally, for example, called in the program:

Copy the code
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 = 0; i < 5; i++) { Company com = new Company(); com.Name = "A" + i; com.Sale = (i * 10).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 = "20"; Person p2 = new Person(); p2.Name = "B"; p2.Address = "美国"; p2.Age = "21"; 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; } } }
Copy the code

 

Guess you like

Origin www.cnblogs.com/MuNet/p/11487863.html