private void GridViewKnownHands_Sorting(object sender, GridViewSortingEventArgs e) { var reportHands = new List <ReportHandViewModel>(e.DataControl.ItemsSource as IEnumerable <ReportHandViewModel>); if (reportHands == null) { e.Cancel = true; return; } var sorted = SortData(reportHands, e); if (sorted != null) { e.DataControl.SortDescriptors.Clear(); var collection = (DataContext as ReportGadgetViewModel).FilteredReportSelectedItemStatisticsCollection; for (int i = 0; i < sorted.Count(); i++) { collection.Move(collection.IndexOf(sorted.ElementAt(i)), i); } e.Cancel = true; } }
private void AssociatedObject_Sorting(object sender, GridViewSortingEventArgs e) { e.Cancel = true; var sortByPropertyName = (( GridViewDataColumn )e.Column).DataMemberBinding.Path.Path; switch (e.OldSortingState) { case SortingState.None: e.NewSortingState = SortingState.Ascending; SortDirection = ListSortDirection.Ascending; SortBy = sortByPropertyName; break; case SortingState.Ascending: e.NewSortingState = SortingState.Descending; SortDirection = ListSortDirection.Descending; SortBy = sortByPropertyName; break; case SortingState.Descending: SortDirection = ListSortDirection.Ascending; SortBy = sortByPropertyName; break; default: break; } // Sort executed server-side. SortCommand.Execute(null); }
private void Sorting(object sender, GridViewSortingEventArgs e) { if (e.Column.UniqueName == "CustomerID") { e.Cancel = true; ColumnSortDescriptor descriptor = (from d in grid.SortDescriptors.OfType <ColumnSortDescriptor>() where object.Equals(d.Column, grid.Columns["CompanyName"]) select d).FirstOrDefault(); if (descriptor == null) { grid.SortDescriptors.Add(new ColumnSortDescriptor() { Column = grid.Columns["CompanyName"], SortDirection = ListSortDirection.Ascending }); e.Column.SortingState = SortingState.Ascending; } else { descriptor.SortDirection = descriptor.SortDirection == ListSortDirection.Ascending ? ListSortDirection.Descending : ListSortDirection.Ascending; e.Column.SortingState = descriptor.SortDirection == ListSortDirection.Ascending ? SortingState.Ascending : SortingState.Descending; } } }
private IEnumerable <T> SortData <T>(IEnumerable <T> collection, GridViewSortingEventArgs e) { var columnValues = collection.Select(x => ReflectionHelper.GetMemberValue(x, (e.Column as GridViewDataColumn).GetDataMemberName())); IEnumerable <T> result = null; if (columnValues.Any(x => x == null || string.IsNullOrEmpty(x.ToString()))) { return(result); } decimal decValue = 0; if (!columnValues.Any(x => !Decimal.TryParse(x.ToString(), out decValue))) { if (e.OldSortingState == SortingState.None) { e.NewSortingState = SortingState.Ascending; result = collection.OrderBy(x => Decimal.Parse(ReflectionHelper.GetMemberValue(x, (e.Column as GridViewDataColumn).GetDataMemberName()).ToString())).ToList(); } else if (e.OldSortingState == SortingState.Ascending) { e.NewSortingState = SortingState.Descending; result = collection.OrderByDescending(x => Decimal.Parse(ReflectionHelper.GetMemberValue(x, (e.Column as GridViewDataColumn).GetDataMemberName()).ToString())).ToList(); } else { e.NewSortingState = SortingState.None; result = collection.OrderByDescending(x => Math.Abs(Decimal.Parse(ReflectionHelper.GetMemberValue(x, (e.Column as GridViewDataColumn).GetDataMemberName()).ToString()))).ToList(); } } return(result); }
private void Sorting(object sender, GridViewSortingEventArgs e) { if (e.Column.UniqueName == "CustomerID") { e.Cancel = true; ColumnSortDescriptor descriptor = (from d in grid.SortDescriptors.OfType<ColumnSortDescriptor>() where object.Equals(d.Column, grid.Columns["CompanyName"]) select d).FirstOrDefault(); if (descriptor == null) { grid.SortDescriptors.Add(new ColumnSortDescriptor() { Column = grid.Columns["CompanyName"], SortDirection = ListSortDirection.Ascending }); e.Column.SortingState = SortingState.Ascending; } else { descriptor.SortDirection = descriptor.SortDirection == ListSortDirection.Ascending ? ListSortDirection.Descending : ListSortDirection.Ascending; e.Column.SortingState = descriptor.SortDirection == ListSortDirection.Ascending ? SortingState.Ascending : SortingState.Descending; } } }
private void GridViewReport_Sorting(object sender, GridViewSortingEventArgs e) { var reports = new List <ReportIndicators>(e.DataControl.ItemsSource as IEnumerable <ReportIndicators>); if (reports == null) { e.Cancel = true; return; } var sorted = SortData(reports, e); if (sorted != null) { e.DataControl.SortDescriptors.Clear(); var collection = (DataContext as ReportGadgetViewModel).ReportCollection; // Remove and reassign binding to not affect performance // (In case if SelectedItem index changed it will lose selection so HandsGrid will be unloaded) var binding = BindingOperations.GetBinding(e.DataControl, GridViewDataControl.SelectedItemProperty); BindingOperations.ClearBinding(e.DataControl, GridViewDataControl.SelectedItemProperty); for (int i = 0; i < sorted.Count(); i++) { collection.Move(collection.IndexOf(sorted.ElementAt(i)), i); } BindingOperations.SetBinding(e.DataControl, GridViewDataControl.SelectedItemProperty, binding); e.Cancel = true; } }
private void dgRegionBreakdown_Sorting(object sender, GridViewSortingEventArgs e) { List <RegionBreakdownData> datasource = (e.DataControl.ItemsSource as ObservableCollection <RegionBreakdownData>).ToList(); DataItemCollection collection = (sender as RadGridView).Items; List <RegionBreakdownData> data = new List <RegionBreakdownData>(); foreach (RegionBreakdownData item in collection) { data.Add(item); } if ((e.Column as GridViewDataColumn).DataMemberBinding.Path.Path == "Security" || e.NewSortingState == SortingState.None) { ArrangeSortOrder(data); return; } List <String> distinctRegions = data.Select(a => a.Region).Distinct().ToList(); List <OrderedAggregates> orderedRegion = GetRegionAggregates(data, distinctRegions, (e.Column as GridViewDataColumn).DataMemberBinding.Path.Path); orderedRegion = e.NewSortingState == SortingState.Ascending ? orderedRegion.OrderBy(a => a.Aggregate).ToList() : orderedRegion.OrderByDescending(a => a.Aggregate).ToList(); int regionCount = 1; foreach (String region in orderedRegion.Select(a => a.Description)) { List <string> distinctCountries = data.Where(a => a.Region == region).OrderBy(a => a.Country) .Select(a => a.Country).Distinct().ToList(); List <OrderedAggregates> orderedCountnies = GetCountryAggregates(data, distinctCountries, region, (e.Column as GridViewDataColumn).DataMemberBinding.Path.Path); orderedCountnies = e.NewSortingState == SortingState.Ascending ? orderedCountnies.OrderBy(a => a.Aggregate).ToList() : orderedCountnies.OrderByDescending(a => a.Aggregate).ToList(); int countryCount = 1; foreach (String country in orderedCountnies.Select(a => a.Description)) { foreach (RegionBreakdownData item in datasource) { if (item.Region == region && item.Country == country) { item.RegionSortOrder = String.Format("{0}. {1}", regionCount < 10 ? " " + regionCount.ToString() : regionCount.ToString("00"), region); item.CountrySortOrder = String.Format("{0}. {1}", countryCount < 10 ? " " + countryCount.ToString() : countryCount.ToString("00"), country); } } countryCount++; } regionCount++; } this.dgRegionBreakdown.ItemsSource = new ObservableCollection <RegionBreakdownData>(datasource); }
private void dgSectorBreakdown_Sorting(object sender, GridViewSortingEventArgs e) { List <SectorBreakdownData> datasource = (e.DataControl.ItemsSource as ObservableCollection <SectorBreakdownData>).ToList(); DataItemCollection collection = (sender as RadGridView).Items; List <SectorBreakdownData> data = new List <SectorBreakdownData>(); foreach (SectorBreakdownData item in collection) { data.Add(item); } if ((e.Column as GridViewDataColumn).DataMemberBinding.Path.Path == "Security" || e.NewSortingState == SortingState.None) { ArrangeSortOrder(data); return; } List <String> distinctSectors = data.Select(a => a.Sector).Distinct().ToList(); List <OrderedAggregates> orderedSectors = GetRegionAggregates(data, distinctSectors, (e.Column as GridViewDataColumn).DataMemberBinding.Path.Path); orderedSectors = e.NewSortingState == SortingState.Ascending ? orderedSectors.OrderBy(a => a.Aggregate).ToList() : orderedSectors.OrderByDescending(a => a.Aggregate).ToList(); int sectorCount = 1; foreach (String sector in orderedSectors.Select(a => a.Description)) { List <string> distinctIndustries = data.Where(a => a.Sector == sector).OrderBy(a => a.Industry) .Select(a => a.Industry).Distinct().ToList(); List <OrderedAggregates> orderedIndustries = GetCountryAggregates(data, distinctIndustries, sector, (e.Column as GridViewDataColumn).DataMemberBinding.Path.Path); orderedIndustries = e.NewSortingState == SortingState.Ascending ? orderedIndustries.OrderBy(a => a.Aggregate).ToList() : orderedIndustries.OrderByDescending(a => a.Aggregate).ToList(); int industryCount = 1; foreach (String country in orderedIndustries.Select(a => a.Description)) { foreach (SectorBreakdownData item in datasource) { if (item.Sector == sector && item.Industry == country) { item.SectorSortOrder = String.Format("{0}. {1}", sectorCount < 10 ? " " + sectorCount.ToString() : sectorCount.ToString("00"), sector); item.IndustrySortOrder = String.Format("{0}. {1}", industryCount < 10 ? " " + industryCount.ToString() : industryCount.ToString("00"), country); } } industryCount++; } sectorCount++; } this.dgSectorBreakdown.ItemsSource = new ObservableCollection <SectorBreakdownData>(datasource); }
private void clubsGrid_Sorting(object sender, GridViewSortingEventArgs e) { if (e.NewSortingState == SortingState.Ascending) { model.MySortingToolTip = "Ascending Order"; } else if (e.NewSortingState == SortingState.Descending) { model.MySortingToolTip = "Descending Order"; } else { model.MySortingToolTip = ""; } }
private void dataGrid1_Sorting(object sender, GridViewSortingEventArgs e) { //Gets the value of the ItemsSource property as IEnumerable. IEnumerable <Idea> ideas = e.DataControl.ItemsSource as IEnumerable <Idea>; //checks if the value of the collection is null if (ideas == null) { e.Cancel = true; return; } if (e.OldSortingState == SortingState.None) { e.NewSortingState = SortingState.Ascending; ideas = ideas.OrderBy(idea => idea.GetType() .GetProperty((e.Column as GridViewDataColumn).GetDataMemberName()) .GetValue(idea, null)); } //If the sorting state is none, sort the items descending. else if (e.OldSortingState == SortingState.Ascending) { e.NewSortingState = SortingState.Descending; ideas = ideas.OrderByDescending(idea => idea.GetType() .GetProperty((e.Column as GridViewDataColumn).GetDataMemberName()) .GetValue(idea, null)); } //If the sorting state is descending, apply default sorting to the items. else { e.NewSortingState = SortingState.Ascending; ideas = ideas.OrderBy(idea => idea.ideaID); ideas = ideas.OrderBy(idea => idea.ideaStatus); ideas = ideas.OrderBy(idea => idea.costType); ideas = ideas.OrderBy(idea => idea.totalSave); } //Set the sorted collection as source of the RadGridView e.DataControl.ItemsSource = ideas.ToList(); e.Cancel = true; }
private void radGridView1_Sorting(object sender, GridViewSortingEventArgs e) { if (_sObjectDef.Paging && _sObjectDef.TotalRecords > _sObjectDef.RecordsPerPage) { e.Cancel = true; //Have to get the Query name of the field - this is what actually goes in the OrderBy (this will be diferent if its a relation eg OWNER_NAME and OWNER.NAME) //need the name as well so we can show the sort indicator on the column once we have the databack string qField = _sObjectDef.GetField(e.Column.UniqueName).QueryField; if (e.OldSortingState == SortingState.None) { _sObjectDef.SortColumn = e.Column.UniqueName; _sObjectDef.SortQueryField = qField; _sObjectDef.SortDir = "ASC"; } else if (e.OldSortingState == SortingState.Ascending) { _sObjectDef.SortColumn = e.Column.UniqueName; _sObjectDef.SortQueryField = qField; _sObjectDef.SortDir = "DESC"; } else { _sObjectDef.SortColumn = ""; _sObjectDef.SortQueryField = ""; _sObjectDef.SortDir = ""; } _sObjectDef.CurrnetPage = 0; LoadData(false); } }
private void Sorting(object sender, GridViewSortingEventArgs e) { e.Cancel = true; var column = _grid.Columns[e.Column.UniqueName]; if (column == null) return; var pageableList = _grid.DataContext as IPageableList; if (pageableList == null) return; //initial sorting state is Ascending var direction = SortDirection.Ascending; //if there is no current sorting, then it will be created if (pageableList.SortDescriptors != null && pageableList.SortDescriptors.Count > 0) { //will be null if current column is not sorted var currentSorting = pageableList.SortDescriptors.FirstOrDefault(x => x.ColumnName == column.UniqueName); //current column is already sorted if (currentSorting != null) { //sorting state is being changed (from Ascending to Descending, //from Descending to None) switch (currentSorting.Direction) { case SortDirection.Ascending: direction = SortDirection.Descending; break; case SortDirection.Descending: direction = SortDirection.Ascending; break; } } if (_isShiftKeyPressed) { if (currentSorting != null) { if (direction != SortDirection.None) { var currentSortingIndex = pageableList.SortDescriptors.IndexOf(currentSorting); pageableList.SortDescriptors.RemoveAt(currentSortingIndex); pageableList.SortDescriptors.Insert(currentSortingIndex, new SortDescriptor(currentSorting.ColumnName, direction)); } else pageableList.SortDescriptors.Remove(currentSorting); } else pageableList.SortDescriptors.Add(new SortDescriptor(column.UniqueName, direction)); } else { var list = pageableList.SortDescriptors.Where(x => !x.IsGrouped).ToList(); for (var i = list.Count() - 1; i >= 0 ; i--) pageableList.SortDescriptors.Remove(list[i]); if (direction != SortDirection.None) pageableList.SortDescriptors.Add(new SortDescriptor(column.UniqueName, direction)); } pageableList.Refresh(pageableList.CurrentPageNumber); _sortDescriptors = pageableList.SortDescriptors; } else { var sortDescriptors = new SortList { new SortDescriptor(column.UniqueName, direction) }; pageableList.Refresh(sortDescriptors); _sortDescriptors = sortDescriptors; } SortingState state; if (!Enum.TryParse(direction.ToString(), out state)) return; column.SortingState = state; column.SortingStateChanged -= Sorted; column.SortingStateChanged += Sorted; }
public virtual void ControlSorting(Object sender, GridViewSortingEventArgs e) { // Sorting happens on server-side, hence the the own sorting of the UIControl must be // canceled and therefore, the SortingStates must be switched manually here. if (e.OldSortingState == SortingState.None) { e.NewSortingState = SortingState.Ascending; } else if (e.OldSortingState == SortingState.Ascending) { e.NewSortingState = SortingState.Descending; } else if (e.OldSortingState == SortingState.Descending) { e.NewSortingState = SortingState.None; } List <ISortDescriptor> sortDescriptorList = new List <ISortDescriptor>(); bool modifiedOrRemoved = false; // Not only the EventArgs, but all SortDescriptors set on the Control must be evaluated. // (E.g. in a RadGridView, the user can specify multiple descriptors by holding the shift key) // As the Controls own sorting is canceled, the descriptors are not directly available from the // sender and must be tracked manually. if (!e.IsMultipleColumnSorting) { if (SortDescriptorList.Count > 0) { SortDescriptorList.Clear(); modifiedOrRemoved = true; } } // The MemberName of the property to sort is currently still available as SortPropertyName in the // EventArgs, but it is marked as obsolet and thus removed in future versions. Therefore the PropertyName // will be evaluated by casting the the Column to a GridViewBoundColumnBase and calling its // GetDataMemberName() method. String sortPropertyName = ((GridViewBoundColumnBase)e.Column).GetDataMemberName(); AmbethISortDescriptor ambethDescriptor = SortDescriptorConverter.ConvertTelerikSortingState(e.NewSortingState, sortPropertyName); foreach (AmbethSortDescriptor sortDescriptor in SortDescriptorList) { if (sortDescriptor.Member != sortPropertyName) { continue; } if (ambethDescriptor != null) { sortDescriptor.SortDirection = ambethDescriptor.SortDirection; ambethDescriptor = null; } else { SortDescriptorList.Remove(sortDescriptor); // OK to remove directly, because break is following } modifiedOrRemoved = true; break; } if (ambethDescriptor != null) { SortDescriptorList.Add(ambethDescriptor); modifiedOrRemoved = true; } if (modifiedOrRemoved) { OnPropertyChanged("SortDescriptorList"); } // As sorting is canceled for the control, the status of all other descriptors must be reset manually: //DisplayCurrentSortStates(); e.Cancel = true; // Prevent the UIControl from own sorting }
private void grdUserList_Sorting(object sender, GridViewSortingEventArgs e) { e.Cancel = true; DelegateEventVehicle.SetGridSort(e.Column.Header.ToString(), string.Empty); }
void CustomGridView_Sorting(object sender, GridViewSortingEventArgs e) { if (this.Columns["IsNew"] != null) { e.Cancel = true; this.SortDescriptors.Clear(); this.SortDescriptors.Add(new ColumnSortDescriptor() { Column = this.Columns["IsNew"], SortDirection = ListSortDirection.Descending, }); this.Columns["IsNew"].SortingState = SortingState.Descending; this.SortDescriptors.Add(new ColumnSortDescriptor() { Column = e.Column, SortDirection = e.NewSortingState == SortingState.Descending ? ListSortDirection.Descending : ListSortDirection.Ascending, }); } else if (e.NewSortingState == SortingState.None) { e.NewSortingState = SortingState.Ascending; } }