简体   繁体   English

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

[英]Sort dataGridView columns in C# ? (Windows Form)

I have a datagridview that i bind from an sql table, in that dv i have those attributes: Id, Name and Price.我有一个从 sql 表绑定的 datagridview,在那个 dv 中我有这些属性:Id、Name 和 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).当我将名称列的 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.我正在寻找一些代码,可以在加载表单时完成这项工作。

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进行排序。

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

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. 如果从应用程序执行SQL查询,请订购返回的数据。 For example, make a function that calls the procedure or executes the SQL and give it a parameter that gets the orderby criteria. 例如,创建一个调用过程或执行SQL的函数,并为其提供一个获取orderby条件的参数。 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. 因为如果您订购了从数据库返回的数据,它将消耗时间,但是它会被执行,因为它表示您希望它不是从您希望在运行时订购的UI中进行排序,因此在执行SQL时对其进行排序查询。

This one is simplier :) 这个更简单:)

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

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;

Sorry for my bad english. 对不起,我的英语不好。

I know 2 solutions to this problem.我知道这个问题的 2 个解决方案。

1. Sort (DataGridViewColumn column, ListSortDirection direction) function 1.排序(DataGridViewColumn列,ListSortDirection方向)功能

This function can be used to sort a column alphabetically or numerically or by date in descending or ascending order.此函数可用于按字母顺序或数字顺序或按日期以降序或升序对列进行排序。

example:例子:

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

2. Sort (IComparer comparer) function 2.排序(IComparer comparer)功能

This function can be use for all other situations as此功能可用于所有其他情况,如

Sorting a column with specific order that is different than numeric order (example: sorting an amount that can be negative or positive using only absolute values)以不同于数字顺序的特定顺序对列进行排序(例如:仅使用绝对值对可以为负或正的数量进行排序)

Sorting a column with specific order that is different than alphabetic order (example: sorting a text using case insensitive sort)以不同于字母顺序的特定顺序对列进行排序(例如:使用不区分大小写的排序对文本进行排序)

Sorting multiples columns as sorting a table using AMOUNT column as first column and DATE column as second column.将多个列排序为使用 AMOUNT 列作为第一列和 DATE 列作为第二列对表进行排序。

VB.Net example: VB.Net 示例:

Private Sub pbSort_Click(sender As Object, e As EventArgs) _ 
    Handles pbSort.Click

    grid.Sort(New AmountDateComparer(Me))
End Sub

Private Class AmountDateComparer : Implements IComparer

    Private frm As FrmSearch

    Public Sub New(frm As FrmSearch)
        Me.frm = frm
    End Sub

    Public Function Compare(x1 As Object, x2 As Object) As Integer _
        Implements IComparer.Compare

        Dim row1 As DataGridViewRow = x1
        Dim row2 As DataGridViewRow = x2

        ' compare AMOUNT values of columns

        Dim nAmount1 = Convert.ToDecimal(row1.Cells(frm.Col_AMOUNT.Index).Value)
        Dim nAmount2 = Convert.ToDecimal(row2.Cells(frm.Col_AMOUNT.Index).Value)

        Dim iCompareResult As Integer 
            = System.Decimal.Compare(nAmount1, nAmount2)

        If iCompareResult = 0 Then
            'compare DATE values of columns
            Dim d1 = Convert.ToDateTime(row1.Cells(frm.Col_DATE.Index).Value)
            Dim d2 = Convert.ToDateTime(row2.Cells(frm.Col_DATE.Index).Value)

            iCompareResult = System.DateTime.Compare(d1, d2)
        End If

        Return iCompareResult
    End Function
End Class

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

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

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