简体   繁体   English

C# Winforms DataGridView 具有排序/过滤功能,如 Ms Excel

[英]C# Winforms DataGridView with sorting/filtering like Ms Excel

Hi I need a quick solution to do filtering/sorting using the Winforms DataGridView control just as in Excel.嗨,我需要一个快速的解决方案来使用 Winforms DataGridView 控件进行过滤/排序,就像在 Excel 中一样。

I have reviewed the existing posts on this area but none seems to meet my needs.我已经查看了有关该领域的现有帖子,但似乎没有一个能满足我的需求。

I am populating my DataGridView manually - no data binding我正在手动填充我的 DataGridView - 没有数据绑定

The DataGridView columns already support sorting. DataGridView 列已经支持排序。

I would populate a DataTable with your data and then bind the DataGridView to myDataTable.DefaultView.我会用您的数据填充 DataTable,然后将 DataGridView 绑定到 myDataTable.DefaultView。

You can filter the rows displayed by setting myDataTable.DefaultView.RowFilter.您可以通过设置 myDataTable.DefaultView.RowFilter 来过滤显示的行。

You could place Textboxes and/or Comboboxes above the DataGridView and update myDataTable.DefaultView.RowFilter as the input/selections change.您可以将文本框和/或组合框放在 DataGridView 上方,并在输入/选择更改时更新 myDataTable.DefaultView.RowFilter。

如果您正在寻找类似过滤功能的 Excel,请查看这篇文章: http : //msdn.microsoft.com/en-us/library/aa480727.aspx

Why don't use a cheap 3rd-party component?为什么不使用便宜的 3rd 方组件? Even if you buy it, eventually it could really save your money.即使您购买了它,最终它也确实可以为您省钱。 This DataGridView alternative with autofilter works very fast, and unbound mode is its main work mode.这个带有自动过滤器的 DataGridView 替代品工作得非常快,未绑定模式是它的主要工作模式。 Plus it supports Excel-style AutoFilter.此外,它还支持 Excel 风格的自动筛选。

Do you want something like this?你想要这样的东西吗?

Excel过滤

DataGridView-AutoFilter is a ready-made Nuget package, you just need to download it and follow this article, it is an easy and enhanced approach. DataGridView中,自动筛选是一个现成的NuGet包,你只需要下载它,并按照文章,它是一个简单的和增强的方法。

Microsoft has created a sample project for VB and C# where they show how to create what they say is an "auto-filter" plugin. Microsoft为 VB 和 C# 创建了一个示例项目,其中展示了如何创建他们所说的“自动过滤器”插件。 I personally do not like it as it allows filter exact only so product and customer will work, open invoice where price > some value is not implemented我个人不喜欢它,因为它只允许精确过滤,这样产品和客户才能工作,打开发票,价格 > 某些价值未实施

Sorting is implemented by using:排序是通过使用实现的:

foreach (DataGridViewColumn column in MyDataView.Columns)
{
      column.SortMode = DataGridViewColumnSortMode.Automatic;
}

You do this when the grid has it's columns, if auto creating the columns… after binding.当网格有它的列时,你这样做,如果自动创建列......绑定后。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM