Results 1 to 4 of 4

Thread: [RESOLVED] [2005] Sorting a DataGridView before showing it

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Mar 2005
    Location
    Sector 001
    Posts
    1,577

    Resolved [RESOLVED] [2005] Sorting a DataGridView before showing it

    I am creating my own datatable to feed a DataGridView but the initial sorting(if there is one) is not the one that I need. I am re-sorting it afterwards but it takes some time. Is it possible to somehow provide an already sorted stuff prior to the visual fill?

    This is how I am doing it now:
    VB Code:
    1. GridFiles.DataSource = workTable
    2. GridFiles.Sort(GridFiles.Columns("Website"), System.ComponentModel.ListSortDirection.Ascending)
    VB 2005, Win Xp Pro sp2

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Sorting a DataGridView before showing it

    Bind the DataTable to a BindingSource, set the Sort property of the BindingSource, bind the BindingSource to the grid.

    Better yet, use an ORDER BY clause in your SQL query to get the data in the order you need in the first place.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Mar 2005
    Location
    Sector 001
    Posts
    1,577

    Re: [2005] Sorting a DataGridView before showing it

    Thanks, I tried workBinding.Sort = "Website ASC" and is a bit faster now.

    There is no SQL source, I am building the table with
    VB Code:
    1. workTable.Columns.Add("Website", Type.GetType("System.String"))
    2. etc...
    My table is just not coming out sorted the way I'd like it to be.
    VB 2005, Win Xp Pro sp2

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Sorting a DataGridView before showing it

    If you're adding each row manually then it will be in the order that you add the rows. If you want the rows in a different order then you can add them in a different order. Having said that, sorting via the BindingSource is just as easy.

    Also, if you didn't want to use a BindingSource for some reason you could have just set the DefaultView.Sort property of the DataTable. I'd stick with the BindingSource though.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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