Results 1 to 12 of 12

Thread: DataGridView sorting

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2011
    Posts
    184

    DataGridView sorting

    I'm trying to sort my table by columns. None of these command seems to work. Is it possible to sort by two columns.

    DataGridView2.Sort(1, "Ascending")
    DataGridView2.Sort(DataGridView2.SelectedColumns(1), "Ascending")

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,376

    Re: DataGridView sorting

    <waiting on google...>
    C# using a binding source - here

    vb.net using a datatable - here

    vb.net question with a c# answere - here
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  3. #3

    Re: DataGridView sorting

    When in doubt, consult the official MSDN website (or search engine of your flavor) for sorting a DataGridView. Surprisingly (or not), I found that via this simple search as the first result.

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Aug 2011
    Posts
    184

    Re: DataGridView sorting

    dday9 - your solution sorts the table, not the DataGridView.
    formlesstree4 - your response brings me back to my code which I'm having problems.

    I believe this is the correct code to sort the DataGridView. The parameters are not correct.
    DataGridView2.Sort(1, "Ascending")
    DataGridView2.Sort(DataGridView2.SelectedColumns(1), "Ascending")

  5. #5

    Re: DataGridView sorting

    Quote Originally Posted by holisticsam View Post
    dday9 - your solution sorts the table, not the DataGridView.
    formlesstree4 - your response brings me back to my code which I'm having problems.

    I believe this is the correct code to sort the DataGridView. The parameters are not correct.
    DataGridView2.Sort(1, "Ascending")
    DataGridView2.Sort(DataGridView2.SelectedColumns(1), "Ascending")
    How are they not correct? Do you get an error message or does nothing happen?

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Aug 2011
    Posts
    184

    Re: DataGridView sorting

    DataGridView2.Sort(1, "Ascending")
    -- syntax error
    DataGridView2.Sort(DataGridView2.SelectedColumns(1), "Ascending")
    -- Message="Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index"
    (5 column table)

  7. #7

    Re: DataGridView sorting

    If you had read the documentation for the Sort method, you would realize why you're getting the error you are:

    There are TWO ways to Sort on a DGV:
    Code:
    Sort(IComparer)
    And
    Code:
    Sort(dataGridViewColumn As DataGridViewColumn, direction As ListSortDirection)
    Nowhere does the Sort routine have an overload that accepts an Integer and String variable type! What you should be doing is close to what your second item is.
    Code:
    DataGridView2.Sort(DataGridView2.Columns(0), System.ComponentModel.ListSortDirection.Ascending)

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Aug 2011
    Posts
    184

    Re: DataGridView sorting

    formlesstree4, thank you for your clarification. I read the info but was unable to understand it. Do you know if it is possible to sort two columns?

  9. #9

    Re: DataGridView sorting

    Quote Originally Posted by holisticsam View Post
    formlesstree4, thank you for your clarification. I read the info but was unable to understand it. Do you know if it is possible to sort two columns?
    From glancing over the documentation, the SortedColumn Property seems to imply only one column can be sorted. However, I could be wrong.

  10. #10
    Member
    Join Date
    Mar 2013
    Posts
    51

    Re: DataGridView sorting

    I really can't get my head around the documentation.
    I even copied and pasted the example from the MSDN website into my code and changed the names of the identifiers to my code and it still didn't work.

  11. #11
    Member
    Join Date
    Mar 2013
    Posts
    51

    Re: DataGridView sorting

    and I get this error DataGridView control cannot be sorted if it is bound to an IBindingList that does not support sorting.

    if I use this. Dim CurrentColumn As DataGridViewColumn = InvoiceDataGrid.CurrentCell.OwningColumn
    Dim direction As ListSortDirection
    direction = ListSortDirection.Ascending
    InvoiceDataGrid.Sort(CurrentColumn, direction)

  12. #12
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,713

    Re: DataGridView sorting


Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width