名单上的LT和排序的DataGridView数据源; T>其中,T是匿名 [英] Sorting Datagridview datasource on List<T> where T is anonymous

查看:87
本文介绍了名单上的LT和排序的DataGridView数据源; T>其中,T是匿名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个相对简单的问题。我有一个DataGridView,而这一切确实是显示的统计数据。有行无编辑/添加/删除。在DataGridView绑定到一个列表。我想要实现的是让用户能够对列进行排序。

A relatively simple question. I have a datagridview, which all it does is displays statistics. There is no editing/adding/deleting of rows. The datagridview is bound to a List. All I want to achieve is to have the user be able to sort the columns.

class Market
{
    public int Location {get;set;}
    public float Value {get;set;}
    //...
}
class City
{
    public String Name {get;set;}
    //...
}

List<Market> _markets;
List<City> _cities;
//Lists are populated.

dataGridView1.DataSource = _markets.Select(market => 
    new { _cities[market.Location].Name, market.Value}).ToList();



如所预期的,该列是不排序,但所显示的信息是什么是想要的。我的问题是如何使DataGridView的排序基于列的类型与最少的代码复杂和最少的,因为代码将在整个被多次使用。

As expected, the columns are not sortable, but the information that is displayed is what is wanted. My question is how to make the DataGridView sort based on the column type with the least complicated and least amount of code, as the code will be used multiple times throughout.

这应用程序使用使用一个数据库,有意见。这些意见,然后填充DataGridViews。的意见是仍然存在,所以一个可能的解决方案可能是:

This application used to use a DataBase that had views. These views then populated the DataGridViews. The views are still around, so a possible solution could be:

DataBase.ViewMarketValue temp = new DataBase.ViewMarketValue()

_markets.ForEach(market => temp.AddViewMarketValueRow(_cities[market.Location].Name, market.Value);
dataGridView1.DataSource = temp;

这导致所需的:具有所有信息的datagridview的,它是可排序的唯一问题是,它似乎是错误的使用意见。在这方面,所以我应该怎么办?

This results in the desired: a datagridview that has all the information and it is sortable. The only problem is that it seems wrong to use views in this aspect. So what should I do?

推荐答案

在为了能够将数据在<$ C自动排序$ C> DataGridView的,你需要一个集合实现 IBindingListView 。在BCL,实现这个接口的唯一类数据视图的BindingSource (但后者只支持排序,如果基础数据源支持的话太)。

In order to be able to sort the data automatically in the DataGridView, you need a collection that implements IBindingListView. In the BCL, the only classes that implement this interface are DataView and BindingSource (but the latter only supports sorting if the underlying datasource supports it too).

所以,你有几种选择:


  • 创建一个数据表来保存数据,并将其绑定到 DataGridView的(它实际上将绑定到默认视图的<$ C的$ C>数据表)

  • 创建自己的集合类,它实现 IBindingListView

  • 使用现有的实现,如 AdvancedList< T> 类张贴由Marc Gravell中的这个帖子。您还需要从您的查询的结果添加一个构造函数来生成列表:

  • create a DataTable to hold the data, and bind it to the DataGridView (it will actually bind to the DefaultView of the DataTable)
  • create your own collection class that implements IBindingListView
  • use an existing implementation, like the AdvancedList<T> class posted by Marc Gravell in this post. You will also need to add a constructor to build the list from the result of your query:

public AdvancedList(IEnumerable<T> collection)
{
    foreach (var item in collection)
    {
        Add(item);
    }
}


由于您的查询的结果是一个匿名的类型,你将不能够直接调用构造函数。要解决这个问题最简单的方法是利用类型推断,通过创建,将创建列表中的泛型方法。为方便起见,你可以把它创建为一个扩展方法:

Since the result of your query is an anonymous type, you won't be able to call the constructor directly. The easiest way to work around the issue is to take advantage of type inference, by creating a generic method that will create the list. For convenience, you can create it as an extension method:

public static AdvancedList<T> ToAdvancedList<T>(this IEnumerable<T> source)
{
    return new AdvancedList<T>(source);
}

您可以使用它这样的:

dataGridView1.DataSource = _markets.Select(market => 
    new { _cities[market.Location].Name, market.Value}).ToAdvancedList();

这篇关于名单上的LT和排序的DataGridView数据源; T&GT;其中,T是匿名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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