Guest User

iGridTest

a guest
Apr 26th, 2014
315
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.90 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Linq;
  6. using System.Reflection;
  7. using System.Windows.Forms;
  8. using TenTec.Windows.iGridLib;
  9.  
  10. namespace iGridTest
  11. {
  12.     public partial class Form1 : Form
  13.     {
  14.         public Form1()
  15.         {
  16.             InitializeComponent();
  17.  
  18.             var context = new BettingAssistantEntities();
  19.             var source = context.bfevents.ToList();
  20.  
  21.             // IEnumerable
  22.             iGrid1.FillWithDataIEnumerable(source);
  23.  
  24.             // DataTable
  25.             //var dataTable = source.ToDataTable();
  26.             //iGrid1.FillWithData(dataTable);
  27.  
  28.             iGrid1.Cols.AutoWidth();
  29.         }
  30.     }
  31.  
  32.     public static class FillWithDataEx
  33.     {
  34.         public static void FillWithDataIEnumerable<T>(this iGrid grid, IEnumerable<T> collection)
  35.         {
  36.             var getType = typeof(T);
  37.  
  38.             foreach (var prop in getType.GetProperties())
  39.             {
  40.                 var col = grid.Cols.Add();
  41.                 col.Text = prop.Name;
  42.                 col.Tag = prop;
  43.             }
  44.  
  45.             foreach (var element in collection)
  46.             {
  47.                 int i = 0;
  48.                 var row = grid.Rows.Add();
  49.                 foreach (iGCol col in grid.Cols)
  50.                 {
  51.                     var property = (PropertyInfo)col.Tag;
  52.                     var objValue = property.GetValue(element, null);
  53.                     row.Cells[i].Value = objValue;
  54.                     i++;
  55.                 }
  56.             }
  57.         }
  58.     }
  59.  
  60.     public static class IEnumerableEx
  61.     {
  62.         public static DataTable ToDataTable<T>(this IEnumerable<T> items)
  63.             where T : class
  64.         {
  65.             //DataTable oReturn = new DataTable(typeof(T).Name);
  66.             DataTable table = new DataTable(typeof(T).Name);
  67.             PropertyInfo[] props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
  68.  
  69.             // Add the properties as columns to the datatable
  70.             foreach (var prop in props)
  71.             {
  72.                 Type propType = prop.PropertyType;
  73.  
  74.                 // Is it a nullable type? Get the underlying type
  75.                 if (propType.IsGenericType && propType.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))
  76.                     propType = new NullableConverter(propType).UnderlyingType;
  77.  
  78.                 table.Columns.Add(prop.Name, propType);
  79.             }
  80.  
  81.             var propGetters = new List<Func<T, object>>();
  82.             foreach (var prop in props)
  83.             {
  84.                 Func<T, object> func = (Func<T, object>)ReflectionUtility.GetGetter(prop);
  85.                 propGetters.Add(func);
  86.             }
  87.  
  88.             // Add the property values per T as rows to the datatable
  89.             foreach (var item in items)
  90.             {
  91.                 var values = new object[props.Length];
  92.                 for (var i = 0; i < props.Length; i++)
  93.                 {
  94.                     //values[i] = props[i].GetValue(item, null);
  95.                     values[i] = propGetters[i](item);
  96.                 }
  97.  
  98.                 table.Rows.Add(values);
  99.             }
  100.  
  101.             return table;
  102.         }
  103.     }
  104.  
  105.     public class ReflectionUtility
  106.     {
  107.         internal static Func<object, object> GetGetter(PropertyInfo property)
  108.         {
  109.             // get the get method for the property
  110.             MethodInfo method = property.GetGetMethod(true);
  111.  
  112.             // get the generic get-method generator (ReflectionUtility.GetSetterHelper<TTarget, TValue>)
  113.             MethodInfo genericHelper = typeof(ReflectionUtility).GetMethod(
  114.                 "GetGetterHelper",
  115.                 BindingFlags.Static | BindingFlags.NonPublic);
  116.  
  117.             // reflection call to the generic get-method generator to generate the type arguments
  118.             MethodInfo constructedHelper = genericHelper.MakeGenericMethod(
  119.                 method.DeclaringType,
  120.                 method.ReturnType);
  121.  
  122.             // now call it. The null argument is because it's a static method.
  123.             object ret = constructedHelper.Invoke(null, new object[] { method });
  124.  
  125.             // cast the result to the action delegate and return it
  126.             return (Func<object, object>)ret;
  127.         }
  128.  
  129.         static Func<object, object> GetGetterHelper<TTarget, TResult>(MethodInfo method)
  130.             where TTarget : class // target must be a class as property sets on structs need a ref param
  131.         {
  132.             // Convert the slow MethodInfo into a fast, strongly typed, open delegate
  133.             Func<TTarget, TResult> func = (Func<TTarget, TResult>)Delegate.CreateDelegate(typeof(Func<TTarget, TResult>), method);
  134.  
  135.             // Now create a more weakly typed delegate which will call the strongly typed one
  136.             Func<object, object> ret = (object target) => (TResult)func((TTarget)target);
  137.             return ret;
  138.         }
  139.     }
  140. }
Advertisement
Add Comment
Please, Sign In to add comment