为什么我的列排序在winforms .NET数据网格? [英] Why wont my column sort in a winforms .NET datagrid?

查看:168
本文介绍了为什么我的列排序在winforms .NET数据网格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个WinForms .NET数据网格,其数据源是一个 List< cLineItem> 。 cLineItem是非常简单的类,其属性如单位(int),描述(字符串)和单位金额(float)。



在代码中,我填充行列表,然后设置数据源:

  dataGridView1.DataSource = lines; 

但是,即使网格中的每一列都设置为可排序, ,当您单击列标题时,它不排序行。

解决方案

在DataGridView中排序默认情况下不起作用,除非您的源显式支持排序。您需要将数据源包装在 SortableBindingList 中。您可以使用此 zip文件中的PropertyComparer.cs和SortableBindingList.cs文件并使用它:

  dataGridView1.DataSource = new SortableBindingList< cLineItem>(lines); 


I have a WinForms .NET datagrid whose data source is a List<cLineItem> called lines. cLineItem is very simple class with properties like units (int), description (string) and unit amount (float).

In code, i populate the list of lines and then set the data source:

dataGridView1.DataSource = lines;

This correctly populates the grid, however, even though each of the columns in the grid are set to Sortable, when you click a column header, it doesnt sort the rows.

解决方案

Sorting in DataGridView doesn't work by default, unless your source explicitly supports sorting. You need to wrap your data source in a SortableBindingList. You can use the files PropertyComparer.cs and SortableBindingList.cs from this zip file and use it like this:

dataGridView1.DataSource = new SortableBindingList<cLineItem>(lines);

这篇关于为什么我的列排序在winforms .NET数据网格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆