public void SortDescriptionTest() { IBindingListView view = (IBindingListView) new DataView(table); ListSortDescriptionCollection col = view.SortDescriptions; ((DataView)view).Sort = ""; col = view.SortDescriptions; Assert.AreEqual(0, col.Count, "#1"); ((DataView)view).Sort = null; col = view.SortDescriptions; Assert.AreEqual(0, col.Count, "#2"); ((DataView)view).Sort = "col1 DESC, col2 ASC"; col = view.SortDescriptions; Assert.AreEqual(2, col.Count, "#3"); Assert.AreEqual("col1", col[0].PropertyDescriptor.Name, "#4"); Assert.AreEqual(ListSortDirection.Descending, col[0].SortDirection, "#5"); Assert.AreEqual("col2", col[1].PropertyDescriptor.Name, "#6"); Assert.AreEqual(ListSortDirection.Ascending, col[1].SortDirection, "#7"); //ApplySort Test IBindingListView view1 = (IBindingListView) new DataView(table); Assert.IsFalse(view.Equals(view1), "#8"); view1.ApplySort(col); Assert.AreEqual("[col1] DESC,[col2]", ((DataView)view1).Sort, "#9"); for (int i = 0; i < view.Count; ++i) { Assert.AreEqual(((DataView)view)[i].Row, ((DataView)view1)[i].Row, "#10" + i); } }
public void BaseSortWithInsertionAndFilter() { IBindingListView sut = CreateBindingListOnBasicCustomersList(); PropertyDescriptor pd = TypeDescriptor.GetProperties(typeof(Customer)).Find("Name", false); sut.ApplySort(pd, ListSortDirection.Ascending); String lastName = ""; foreach (Customer c in sut) { Assert.That(c.Name.CompareTo(lastName) > 0); lastName = c.Name; } sut.Filter = "Age > 30"; sut.Add(new Customer() { Name = "Fernand", Age = 18 }); Assert.That(sut, Has.Count.EqualTo(3)); lastName = ""; foreach (Customer c in sut) { Assert.That(c.Name.CompareTo(lastName) > 0); lastName = c.Name; } sut.RemoveFilter(); lastName = ""; foreach (Customer c in sut) { Assert.That(c.Name.CompareTo(lastName) > 0); lastName = c.Name; } }
public virtual void ApplySort(ListSortDescriptionCollection sorts) { if (!(list is IBindingListView)) { throw new NotSupportedException("This operation requires an IBindingListView."); } IBindingListView iblist_view = (IBindingListView)list; iblist_view.ApplySort(sorts); }
public void BaseSort() { IBindingListView sut = CreateBindingListOnBasicCustomersList(); PropertyDescriptor pd = TypeDescriptor.GetProperties(typeof(Customer)).Find("Name", false); sut.ApplySort(pd, ListSortDirection.Ascending); String lastName = ""; foreach (Customer c in sut) { Assert.That(c.Name.CompareTo(lastName) > 0); lastName = c.Name; } sut.ApplySort(pd, ListSortDirection.Descending); lastName = "ZZZZZZZZZZZZZZZ"; foreach (Customer c in sut) { Assert.That(lastName.CompareTo(c.Name) > 0); lastName = c.Name; } }
void BindingListView_CustomSort(int iColumn) { if (DataSource != null && iColumn >= 0 && iColumn < this.Columns.Count) { ColumnBinding header = this.Columns[iColumn] as ColumnBinding; if (header != null) { IBindingListView view = this.DataSource as IBindingListView; if (view != null) { string fieldName = header.Property != null ? header.Property.Name : header.FieldName; if (!string.IsNullOrEmpty(fieldName)) { // handle existing sorts if (view.IsSorted) { ListSortDescription[] arr = new ListSortDescription[view.SortDescriptions.Count]; view.SortDescriptions.CopyTo(arr, 0); bool found = false; for (int idx = 0; idx < arr.Length; ++idx) { ListSortDescription desc = arr[idx]; if (desc.PropertyDescriptor.Name == fieldName) { found = true; if (idx == 0) { if (desc.SortDirection == ListSortDirection.Descending) { desc.SortDirection = ListSortDirection.Ascending; } else { List <ListSortDescription> list = new List <ListSortDescription>(arr); list.Remove(desc); arr = list.ToArray(); } } else { List <ListSortDescription> list = new List <ListSortDescription>(arr); list.Remove(desc); list.Insert(0, desc); desc.SortDirection = ListSortDirection.Ascending; arr = list.ToArray(); } break; } } if (!found) { List <ListSortDescription> list = new List <ListSortDescription>(arr); list.Insert(0, new ListSortDescription(header.Property, ListSortDirection.Descending)); while (list.Count > 3) { list.RemoveAt(list.Count - 1); } arr = list.ToArray(); } view.ApplySort(new ListSortDescriptionCollection(arr)); } else { view.ApplySort(header.Property, ListSortDirection.Ascending); } } } } } }
public void ApplySort(ListSortDescriptionCollection sorts) { source.ApplySort(sorts); }