private void dgOrders_Sorting(object sender, DataGridSortingEventArgs e) { try { string sortPath = e.Column.SortMemberPath; var items = this.dgOrders.ItemsSource as OrderViewModel[]; if (items == null || items.Length < 1) { return; } var sortType = e.Column.SortDirection == null ? ListSortDirection.Ascending : (e.Column.SortDirection == ListSortDirection.Ascending ? ListSortDirection.Descending : ListSortDirection.Ascending); List <OrderViewModel> newVms = null; EnumerableKeySelector selector = new EnumerableKeySelector(items[0].GetType(), sortPath); if (sortType == ListSortDirection.Ascending) { newVms = items.OrderBy(obj => selector.GetData(obj)).ToList(); } else { newVms = items.OrderByDescending(obj => selector.GetData(obj)).ToList(); } this.dgOrders.ItemsSource = newVms.ToArray(); e.Column.SortDirection = sortType; e.Handled = true; } catch (Exception ex) { MessageBox.Show(ex.Message); } }
private void DgvOrders_Sorting(object sender, DataGridSortingEventArgs e) { try { string sortPath = e.Column.SortMemberPath; if (this.orders.Count < 1) { return; } var sortType = e.Column.SortDirection == null ? ListSortDirection.Ascending : (e.Column.SortDirection == ListSortDirection.Ascending ? ListSortDirection.Descending : ListSortDirection.Ascending); List <OrderViewModel> newVms = null; EnumerableKeySelector selector = new EnumerableKeySelector(orders[0].GetType(), sortPath); if (sortType == ListSortDirection.Ascending) { newVms = orders.OrderBy(obj => selector.GetData(obj)).ToList(); } else { newVms = orders.OrderByDescending(obj => selector.GetData(obj)).ToList(); } this.orders.Clear(); foreach (var v in newVms) { this.orders.Add(v); } e.Column.SortDirection = sortType; e.Handled = true; } catch (Exception ex) { MessageBox.Show(ex.Message); } }
private void SortData(PrintOrderPageViewModel p, DataGrid dg, DataGridColumn col) { if (dg == null) { MessageBox.Show("事件源不是DataGrid无法排序"); return; } string sortPath = col.SortMemberPath; if (p.OrderViewModels.Count < 1) { return; } var sortType = col.SortDirection == null ? ListSortDirection.Ascending : (col.SortDirection == ListSortDirection.Ascending ? ListSortDirection.Descending : ListSortDirection.Ascending); List <PrintOrderViewModel> newVms = null; if (sortPath.Contains("DoorNumber")) { //两次排序,第一次根据门牌号排,然后根据货号排序,然后根据付款时间 类型PrintOrderViewModel实际了比较接口 List <PrintOrderViewModel> tmpList = p.OrderViewModels.ToList(); tmpList.Sort(); tmpList.Sort(); tmpList.Sort(); newVms = tmpList; } else { EnumerableKeySelector selector = new EnumerableKeySelector(p.OrderViewModels[0].GetType(), sortPath); if (sortType == ListSortDirection.Ascending) { newVms = p.OrderViewModels.OrderBy(obj => selector.GetData(obj)).ToList(); } else { newVms = p.OrderViewModels.OrderByDescending(obj => selector.GetData(obj)).ToList(); } } p.OrderViewModels.Clear(); for (int i = 0; i < newVms.Count; i++) { newVms[i].DefaultBackground = newVms[i].Background = (i % 2 == 0) ? PrintOrderViewModel.DEFAULTBACKGROUND_LIGHTGREEN : PrintOrderViewModel.DEFAULTBACKGROUND_LIGHTPINK; p.OrderViewModels.Add(newVms[i]); } col.SortDirection = sortType; }