code_junkie

Allow user to sort columns from a LINQ query in a DataGridView

Nov 13th, 2011
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.16 KB | None | 0 0
  1. Dim QueryReOrder = From Q In Query _
  2. Where ((0 - Q.Qualifier) / cmbTSStakeValue.Text) <= 0.1 _
  3. Order By Q.Qualifier Descending _
  4. Select Q
  5.  
  6. Dim bs As New BindingSource
  7. bs.DataSource = QueryReOrder
  8. DGFindMatch.DataSource = bs
  9.  
  10. Sort Nothing String
  11. SortProperty Nothing System.ComponentModel.PropertyDescriptor
  12. SupportsAdvancedSorting False Boolean
  13. SupportsChangeNotification True Boolean
  14. SupportsFiltering False Boolean
  15. SupportsSearching False Boolean
  16. SupportsSorting False Boolean
  17.  
  18. //I know that you asked the question in VB.NET but I don't know the syntax that well.
  19. public class SortableBindingList<T> : BindingList<T>
  20. {
  21. //override necessary sort core methods
  22. }
  23.  
  24. SortableBindingList<string> list = new SortableBindingList<string>(QueryReOrder.ToList());
  25.  
  26. //use list as your DataSource now
  27.  
  28. var query = from o in m_ds.Objective
  29.  
  30. join ot in m_ds.ObjectiveType on o.ObjectiveTypeId equals ot.Id
  31. join dst in m_ds.DevelopmentStatusType on o.DevelopmentStatusTypeId equals dst.Id
  32. join rt in m_ds.ResultType on o.PrecedenceResultTypeId equals rt.Id
  33.  
  34. select new
  35. {
  36. o.Id,
  37. type = ot.Description,
  38. precedence = rt.Description,
  39. o.Symbol,
  40. o.Title,
  41. };
  42.  
  43. public static DataTable DataTableFromIEnumerable( IEnumerable ien )
  44. {
  45. DataTable dt = new DataTable();
  46. foreach ( object obj in ien )
  47. {
  48. Type t = obj.GetType();
  49. PropertyInfo[] pis = t.GetProperties();
  50. if ( dt.Columns.Count == 0 )
  51. {
  52. foreach ( PropertyInfo pi in pis )
  53. {
  54. dt.Columns.Add( pi.Name, pi.PropertyType );
  55. }
  56. }
  57.  
  58. DataRow dr = dt.NewRow();
  59. foreach ( PropertyInfo pi in pis )
  60. {
  61. object value = pi.GetValue( obj, null );
  62. dr[ pi.Name ] = value;
  63. }
  64.  
  65. dt.Rows.Add( dr );
  66. }
  67.  
  68. return dt;
  69. }
  70.  
  71. var query = SqlHelper.GetFilteredObjective();
  72. var bs = new BindingSource();
  73. bs.DataSource = Utils.DataTableFromIEnumerable( query );
  74. dgvObjectives.DataSource = bs;
Add Comment
Please, Sign In to add comment