none
Is it possible to sort a DataGridView firstly by Column1, and then Column2? RRS feed

  • Question

  • Is it possible to sort a DataGridView firstly by Column1, and then Column2?

    Over here, I need to Sort by Job_Status desc order first, then by LeadTime Asc order

    So the URGENT jobs with the EARLIEST LeadTime should be at the TOP.

    LeadTime     JobStatus

    5 May          Urgent
    6 May          Urgent
    9 May          Urgent
    11 May          Urgent
    12 May          Urgent
    8 May          On Hold

    Monday, May 6, 2019 1:53 PM

Answers

  • The best way to do this is not via clicking on each column but instead given the DataSource of the DataGridView is a DataTable (can be a DataSet pointing to a DataTable in the DataSet).

    Example

    Dim view As DataView = CType(MyDataGridView.DataSource, DataTable).DefaultView

    view.Sort = "Column1,Column2 ASC"

    view.Sort = "Column1,Column2 DESC"

    If dealing with a BindingSource you could use the Sort method, same syntax as above e.g. SomeBindingSource.Sort = "Column1, Column2".

    The last idea is shown in C#, last reply on the following page, sorry I don't have time to convert it but should be easy enough.

    https://stackoverflow.com/questions/4646873/sort-a-datagridview-on-multiple-columns


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by wk89 Monday, May 6, 2019 2:28 PM
    Monday, May 6, 2019 2:05 PM
    Moderator

All replies

  • The best way to do this is not via clicking on each column but instead given the DataSource of the DataGridView is a DataTable (can be a DataSet pointing to a DataTable in the DataSet).

    Example

    Dim view As DataView = CType(MyDataGridView.DataSource, DataTable).DefaultView

    view.Sort = "Column1,Column2 ASC"

    view.Sort = "Column1,Column2 DESC"

    If dealing with a BindingSource you could use the Sort method, same syntax as above e.g. SomeBindingSource.Sort = "Column1, Column2".

    The last idea is shown in C#, last reply on the following page, sorry I don't have time to convert it but should be easy enough.

    https://stackoverflow.com/questions/4646873/sort-a-datagridview-on-multiple-columns


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by wk89 Monday, May 6, 2019 2:28 PM
    Monday, May 6, 2019 2:05 PM
    Moderator
  • Thanks

    ProjectsBindingSource.Sort() = "Job_Status DESC, Lead_Time"

    Monday, May 6, 2019 2:27 PM