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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1239525/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 14:03:51  来源:igfitidea点击:

C# Winforms DataGridView with sorting/filtering like Ms Excel

c#datagridview

提问by Julius A

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 - 没有数据绑定

采纳答案by xyz

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。

回答by Joao Coelho

If you're looking for a Excel like filtering funcionality, check out this article: http://msdn.microsoft.com/en-us/library/aa480727.aspx

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

回答by TecMan

Why don't use a cheap 3rd-party component? Even if you buy it, eventually it could really save your money. This DataGridView alternative with autofilterworks very fast, and unbound mode is its main work mode. Plus it supports Excel-style AutoFilter.

为什么不使用便宜的 3rd 方组件?即使您购买了它,最终它也确实可以为您省钱。这个带有自动过滤器的 DataGridView 替代品工作得非常快,未绑定模式是它的主要工作模式。此外,它还支持 Excel 风格的自动筛选。

回答by Walter Vehoeven

Microsoft has created a sample projectfor VB and C# where they show how to create what they say is an "auto-filter" plugin. 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

Microsoft为 VB 和 C#创建了一个示例项目,其中展示了如何创建他们所说的“自动过滤器”插件。我个人不喜欢它,因为它只允许精确过滤,这样产品和客户才能工作,打开发票,价格 > 某些价值未实施

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.

当网格有它的列时,你这样做,如果自动创建列......绑定后。