Results 1 to 4 of 4

Thread: [RESOLVED] [2005] Order columns in a datagridview

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2001
    Posts
    1,331

    Resolved [RESOLVED] [2005] Order columns in a datagridview

    Hello,

    Is there a way to manually order the columns in a grid.

    My grid is bound to the datasource at run-time. So I can't change the order in the designer.

    Is there a method to do that in code?

    Many thanks,

    Steve
    steve

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

    Re: [2005] Order columns in a datagridview

    You can change the order in the designer if you want to, assuming that you know what columns there will. Add the columns you want at design time, put them in the order you want and set their DataPropertyName properties so they know what data source columns to bind to at run time.

    Alternatively, once the grid has auto-generated the columns you should be able to use the Remove and Insert methods of the grid's Columns collection, just like you can any other collection, to re-order the items it contains.
    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
    Dec 2001
    Posts
    1,331

    Re: [2005] Order columns in a datagridview

    Private Sub AdjustColumnOrder()

    With customersDataGridView
    .Columns("CustomerID").Visible = False
    .Columns("ContactName").DisplayIndex = 0
    .Columns("ContactTitle").DisplayIndex = 1
    .Columns("City").DisplayIndex = 2
    .Columns("Country").DisplayIndex = 3
    .Columns("CompanyName").DisplayIndex = 4
    End With

    End Sub
    steve

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

    Re: [2005] Order columns in a datagridview

    Shweet.
    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