本文介绍了使用通用列表作为底层源的 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;

我想要做的是允许用户通过单击代表 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;

为此,我将所有列设置为 'Programatically'(在设计器中)然后我手动添加箭头 (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 排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-22 21:37