GridControl Linq

1. 新增数据

GridView的NewItemRowPosition属性设置为Bottom

代码:

 1 List<ObjectType> DetailCollection { get; set; }
 2 private void XXXForm_Load(object sender, EventArgs e) {
 3     DetailCollection = new List<ObjectType>();
 4 }
 5 private void gvDataView_CustomRowCellEditForEditing(object sender, CustomRowCellEditEventArgs e) {
 6     if (e.RowHandle < 0) { //新增数据
 7         DetailCollection.Add(new ObjectType() { AutoID = 0 });
 8         gvDataView.RefreshData();
 9         gvDataView.FocusedRowHandle = DetailCollection.Count - 1;
10     }
11 }
12 // 删除行
13 private void gvDataView_KeyUp(object sender, KeyEventArgs e) {
14     if (e.KeyCode == Keys.Delete && this.gvDataView.FocusedRowHandle >= 0) {
15         int index = gvDataView.FocusedRowHandle;
16         if (MessageBoxHelper.ShowConfirm("删除不可恢复,是否确定删除?") == DialogResult.OK) {
17             DetailCollection.RemoveAt(index);
18             gvDataView.RefreshData();
19             gvDataView.FocusedRowHandle = DetailCollection.Count > index ? index : -1;
20         }
21     }
22 }

 2. 编辑数据

GridControl可绑定Linq查询的匿名类型结果集,但是不可编辑

代码:

1 var ds = from xc in XXXCollection
2          where ......
3          select new {
4              ACol = xc.XXX,
5              BCol = xc.YYY,
6          };
7 gcDataCtrl.DataSource = ds.ToArray();

注:在绑定的时候,需要结果集ds转换一下,ToArray()/ToList()都可以。

绑定显示不会有问题,但是在编辑的时候就会出现编辑不了的情况。

GridControl不支持匿名类型的编辑,需要将结果集转换一下,这里转换成了DataTable类型。

代码:

1 var ds = from xc in XXXCollection
2          where ......
3          select new {
4              ACol = xc.XXX,
5              BCol = xc.YYY,
6          };
7 gcDataCtrl.DataSource = ds.ConvertToDataTable();

系统库是没有这个函数的,所有需要使用扩展方法。

代码:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Data;
 4 using System.Reflection;
 5 
 6 namespace ManageSystem.Common {
 7     public static class ClassExtension {
 8         #region "Convert Generic List to DataTable"
 9         /// <summary>
10         /// Convert a List{T} to a DataTable.
11         /// </summary>
12         public static DataTable ConvertToDataTable<T>(this IEnumerable<T> items) {
13             var tb = new DataTable(typeof(T).Name);
14             PropertyInfo[] props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
15 
16             foreach (PropertyInfo prop in props) {
17                 Type t = GetCoreType(prop.PropertyType);
18                 tb.Columns.Add(prop.Name, t);
19             }
20 
21             foreach (T item in items) {
22                 var values = new object[props.Length];
23 
24                 for (int i = 0; i < props.Length; i++) {
25                     values[i] = props[i].GetValue(item, null);
26                 }
27                 tb.Rows.Add(values);
28             }
29             return tb;
30         }
31 
32         /// <summary>
33         /// Determine of specified type is nullable
34         /// </summary>
35         public static bool IsNullable(Type t) {
36             return !t.IsValueType || (t.IsGenericType && t.GetGenericTypeDefinition() == typeof(Nullable<>));
37         }
38 
39         /// <summary>
40         /// Return underlying type if type is Nullable otherwise return the type.
41         /// </summary>
42         public static Type GetCoreType(Type t) {
43             if (t != null && IsNullable(t)) {
44                 if (!t.IsValueType) {
45                     return t;
46                 }
47                 else {
48                     return Nullable.GetUnderlyingType(t);
49                 }
50             }
51             else {
52                 return t;
53             }
54         }
55         #endregion
56     }
57 }

 

 

posted @ 2013-12-17 10:32  zhuhc  阅读(364)  评论(0编辑  收藏  举报