允许用户从DataGridView中的LINQ查询中对列进行排序 [英] Allow user to sort columns from a LINQ query in a DataGridView

查看:207
本文介绍了允许用户从DataGridView中的LINQ查询中对列进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不太清楚如何允许在运行时填充DataGridView(当用户点击列标题时),通过BindingSource,来自XML查询的LINQ是DataSource。

  Dim QueryReOrder = From Q In Query _ 
其中((0 - Q.Qualifier)/ cmbTSStakeValue.Text)< = 0.1 _
订单按Q.Qualifier降序_
选择Q

Dim bs As New BindingSource
bs.DataSource = QueryReOrder
DGFindMatch.DataSource = bs

一些DataGridView的属性是:

  Sort Nothing String 
SortProperty Nothing System.ComponentModel.PropertyDescriptor
SupportsAdvancedSorting False Boolean
SupportsChangeNotification True Boolean
SupportsFiltering False Boolean
SupportsSearching False Boolean
Sup portsSorting False Boolean

有没有一个简单的解决方案,可以让用户能够按照点击列标题



谢谢!

解决方案

将LINQ查询的结果转化为支持排序功能的东西。这通常通过从BindingList派生类并在派生类中实现Sorting Core功能来完成。



有很多实例可供选择,这是一个非常简单的实现。 以下是在MSDN上执行此操作的示例。



一旦你实现了所有你需要做的就是把你的结果放在你的数据源中,网格应该允许用户使用列进行排序。 / p>

  //我知道你在VB.NET中提出了这个问题,但我不太了解语法。 
public class SortableBindingList< T> :BindingList< T>
{
//覆盖必需的排序核心方法
}

SortableBindingList< string> list = new SortableBindingList< string>(QueryReOrder.ToList());

//使用列表作为您的DataSource现在


I can't quite work out how to allow a DataGridView populated at runtime to sort (when users click on the column headers) where a LINQ from XML query is the DataSource, via a BindingSource.

    Dim QueryReOrder = From Q In Query _
                       Where ((0 - Q.Qualifier) / cmbTSStakeValue.Text) <= 0.1 _
                       Order By Q.Qualifier Descending _
                       Select Q

    Dim bs As New BindingSource
    bs.DataSource = QueryReOrder
    DGFindMatch.DataSource = bs

Some of the DataGridView's properties are:

Sort            Nothing String
SortProperty                Nothing System.ComponentModel.PropertyDescriptor
SupportsAdvancedSorting         False   Boolean
SupportsChangeNotification      True    Boolean
SupportsFiltering           False   Boolean
SupportsSearching           False   Boolean
SupportsSorting             False   Boolean

Is there a simple solution that will allow a user to be able to sort these values by clicking the column header?

Thanks!

解决方案

You need to get the results of the LINQ query into something supports sorting functionality. This is typically done by deriving a class from BindingList and implementing the Sorting Core functionality in the derived class.

There are many examples of implementations out there to choose from and it is a pretty straight forward thing to implement. Here is an example of doing it on MSDN.

Once you have this implemented all you have to do is put your results in it and use it as your DataSource and the Grid should allow users to sort using the columns.

    //I know that you asked the question in VB.NET but I don't know the syntax that well.
    public class SortableBindingList<T> : BindingList<T>
    {
         //override necessary sort core methods
    }

    SortableBindingList<string> list = new SortableBindingList<string>(QueryReOrder.ToList());

    //use list as your DataSource now

这篇关于允许用户从DataGridView中的LINQ查询中对列进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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