在 C# 中对 dataGridView 列进行排序?(Windows 窗体)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/806725/
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-05 01:59:11  来源:igfitidea点击:

Sort dataGridView columns in C# ? (Windows Form)

c#winformsdatagridviewsortinggrid

提问by AXheladini

I have a datagridview that i bind from an sql table, in that dv i have those attributes: Id, Name and Price. When i set the SortMode of the Name Columns to Automatic and i click on the header of this column i can sort this dv based on the first letter of the Name, this way i can order products based on their first letters ( Acumulator, Boat, CocaCola, Engine etc).

我有一个从 sql 表绑定的 datagridview,在那个 dv 中我有这些属性:Id、Name 和 Price。当我将名称列的 SortMode 设置为自动并单击此列的标题时,我可以根据名称的第一个字母对这个 dv 进行排序,这样我就可以根据产品的第一个字母(Acumulator、Boat、可口可乐、发动机等)。

Is there a way this thing to happen without clicking the header of the column Name. I am looking some code that will do this job when the form will load.

有没有办法在不单击列名称的标题的情况下发生这种情况。我正在寻找一些代码,可以在加载表单时完成这项工作。

采纳答案by BFree

There's a method on the DataGridView called "Sort":

DataGridView 上有一个名为“Sort”的方法:

this.dataGridView1.Sort(this.dataGridView1.Columns["Name"], ListSortDirection.Ascending);

This will programmatically sort your datagridview.

这将以编程方式对您的 datagridview 进行排序。

回答by danish

Use Datatable.Default.Sortproperty and then bind it to the datagridview.

使用Datatable.Default.Sort属性,然后将其绑定到 datagridview。

回答by Ahmy

You can control the data returned from SQL database by ordering the data returned:

您可以通过对返回的数据进行排序来控制从 SQL 数据库返回的数据:

orderby [Name]

If you execute the SQL query from your application, order the data returned. For example, make a function that calls the procedure or executes the SQL and give it a parameter that gets the orderby criteria. Because if you ordered the data returned from database it will consume time but order it since it's executed as you say that you want it to be ordered not from the UI you want it to be ordered in the run time so order it when executing the SQL query.

如果从应用程序执行 SQL 查询,请对返回的数据进行排序。例如,创建一个调用过程或执行 SQL 的函数,并为其提供一个获取 orderby 条件的参数。因为如果你对从数据库返回的数据进行排序,它会消耗时间,但是因为它是执行的,因为你说你希望它不是从 UI 排序你希望它在运行时排序所以在执行 SQL 时排序它询问。

回答by user427483

dataGridView1.Sort(dataGridView1.Columns[0],ListSortDirection.Ascending);

回答by nate wew

This one is simplier :)

这个更简单:)

dataview dataview1; 
this.dataview1= dataset.tables[0].defaultview;
this.dataview1.sort = "[ColumnName] ASC, [ColumnName] DESC";
this.datagridview.datasource = dataview1;

回答by Luis Márquez

The best way to do this is to sort the list before binding data source.

最好的方法是在绑定数据源之前对列表进行排序。

cars = cars.OrderBy(o => o.year).ThenBy(o => o.color).ToList(); adgCars.DataSource = cars;

cars = cars.OrderBy(o => o.year).ThenBy(o => o.color).ToList(); adgCars.DataSource = cars;

Sorry for my bad english.

对不起,我的英语不好。