Tag: sort

数据源不反映DataGridView中的排序

我将DataTable绑定为DataGridView的DataSource 。 我为所有DataGridViewColumn启用了SortMode属性为Automatic 。 但是当我在DataGridView上进行排序时,排序更改没有反映在基础DataSource中,即DataTable。 例如: DataTable table = new DataTable(); table.Columns.Add(“Name”); table.Columns.Add(“Age”, typeof(int)); table.Rows.Add(“Alex”, 27); table.Rows.Add(“Jack”, 65); table.Rows.Add(“Bill”, 22); dataGridView1.DataSource = table; 现在,如果我要获得如下所示的选定行,它将始终返回DataTable中0 Index处的行, 即使在排序后是Alex, 27 。 DataRow newRow = table.NewRow(); newRow.ItemArray = table.Rows[dataGridView1.Rows.IndexOf(dataGridView1.SelectedRows[0])].ItemArray.Clone() as object[]; 有人可以建议我应该如何处理这种情况?

排序自引用关系

假设以下模型。 请注意自引用关系“parent”。 public class Category { public virtual long Id { get; set; } public virtual string Name { get; set; } public virtual Category Parent { get; set; } public virtual long? ParentId { get; set; } } 我的数据如下: id | name | parentId 1——–tag 1 —– null 2——–tag 2 —– 1 3——–tag 3 […]

本机C#支持检查IEnumerable是否已排序?

是否有任何LINQ支持检查IEnumerable是否已排序? 我有一个我要validation的枚举是按非降序排序的,但我似乎无法在C#中找到它的本机支持。 我使用IComparables编写了自己的扩展方法: public static bool IsSorted(this IEnumerable collection) where T : IComparable { Contract.Requires(collection != null); using (var enumerator = collection.GetEnumerator()) { if (enumerator.MoveNext()) { var previous = enumerator.Current; while (enumerator.MoveNext()) { var current = enumerator.Current; if (previous.CompareTo(current) > 0) return false; previous = current; } } } return true; } 一个使用IComparer对象: public static […]

c#Hashtable按键排序

我有一个哈希表,其中键是字母,数值是数字。 如何根据键对哈希表进行排序? ExchangeA, 200 ExchangeV, 100 ExchangeC, 200 就像这样 ExchangeA, 200 ExchangeC, 200 ExchangeV, 100

为什么List .Sort方法重新排序相同的IComparable 元素?

我对List Sort方法如何处理排序有疑问。 鉴于以下要素: class Element : IComparable { public int Priority { get; set; } public string Description { get; set; } public int CompareTo(Element other) { return Priority.CompareTo(other.Priority); } } 如果我尝试这样排序: List elements = new List() { new Element() { Priority = 1, Description = “First” }, new Element() { Priority = 1, Description […]

DataGridView使用SortableBindingList

我有一个返回IList 的函数,它是DataGridView的DataSource。 我了解到DataGridView不会对IList进行排序。 我读了这个stackoverflow Q&A ,我正在尝试实现SortableBindingList。 我一定做错了,因为我的DataGridView是空的。 我还尝试使用TextBox从SortableBindingSource访问一个元素,但也没有。 using Microsoft.SqlServer.Management.Controls; public partial class Form1 : Form { IBusinessLayer businessLayer; IList categories; SortableBindingList catSortable; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { businessLayer = new BusinessLayer(); categories = businessLayer.GetAllCategories(); catSortable = new SortableBindingList(categories); categoryBindingSource.DataSource = catSortable; categoryDataGridView.DataSource = categoryBindingSource; textBox1.Text = catSortable[0].CategoryName; […]

列出复杂排序

我有一个大小的List ,比如XS,S,M,L,XL,XXL,UK 10,UK 12等 我想要的是强制订单是上面的,无论列表中的项目顺序如何,我想我需要一个IComparable运算符,但不确定。 理想情况下,我想让另一个List具有正确的顺序,它可以在列表中引用它的’place’并重新排序,如果它不存在则默认为AZ