C#DataGridView使用Generic List作为基础源进行排序 [英] C# DataGridView sorting with Generic List as underlying source
问题描述
我正在使用 Windows Forms DataGridView 来显示 MyObject
对象的通用列表。
I'm using a Windows Forms DataGridView to display a generic list of MyObject
objects.
首先我将这个集合包装到一个 BindingSource
集合中,然后:
First of all I wrap this collection into a BindingSource
Collection, then:
dataGridView.DataSource = myBindingSource;
我想做的是允许用户通过点击列的标题对列进行排序代表MyObject中的具体属性。
What I want to do is allow the user to sort the columns by clickin on the header of the column representing a concrete Property in MyObject.
我已经阅读了一些文章,我应该在绑定之前进行排序。但是,如果我想要实时排序列表,那么这并不能帮助我。
I've read some articles that I should do sorting before binding. But it does not help me if I want to sort the columns in real time, being said when it's already binded.
问题是,我需要做什么,所以我可以看到DataGridView中的排序箭头,我可以对每列进行排序?
The question is, what exactly do I need to do, so I could see the sorting arrows in DataGridView and I could sort every column ?
推荐答案
我的解决方案是这样的:
My Solution is this:
我自己使用myBindingSource,我在单独的线程中排序,分组..
然后我只需将结果绑定到DataGridView 。
I work with myBindingSource at my own, I do sorting, grouping ..whatever in a separate thread. Then I simply bind the result to the DataGridView.
myDataGridView.DataSource = bindingSource;
为此,我已经将所有要排序的列设置为 '
(在设计师中)
然后我手动添加箭头(ASCENDING / DESCENDING)
通过设置
For this purpose I've setted all the columns to be sorted 'Programatically'
(in designer)
Then I manually add the arrow (ASCENDING / DESCENDING)
by setting
cell.SortGlyphDirection = ... ;
在代码背后。
这篇关于C#DataGridView使用Generic List作为基础源进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!