本文介绍了C#的DataGridView与泛型列表作为底层源代码排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的 Windows窗体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;



我想要做的就是让用户通过clickin在列的标题对列进行排序代表为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工作,我不排序,在一个单独的线程..whatever分组。
然后,我只是结果绑定到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;



为此,我已经设置好的所有列进行排序'编程方式(在设计师)
然后我手动设置

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与泛型列表作为底层源代码排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 09:17