Results 1 to 10 of 10

Thread: [RESOLVED] DGV column ordering / reordering

  1. #1

    Thread Starter
    Fanatic Member Megalith's Avatar
    Join Date
    Oct 2006
    Location
    Secret location in the UK
    Posts
    879

    Resolved [RESOLVED] DGV column ordering / reordering

    Hi all,

    Heres my issue, i use a linq query to pull data from an xml inventory and place it in a a DGV, problem is the column ordering appears to be in the same order as the properties and fields in the row object class and not the order extracted from the xml with LINQ. I suppose i could rearrange the class so that it refects the column ordering i need but i'm wondering is there a way i can control programatically the order of the columns for my DGV? and why does it use the class structure rather than the query structure anyway? the class is done alphabetically as i wrote it with class diagram and class details, the LINQ query is in the order i need it in.
    If debugging is the process of removing bugs, then programming must be the process of putting them in.

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

    Re: DGV column ordering / reordering

    Can you show us your LINQ query? You can set the DisplayIndex of your columns after they're created.
    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
    Fanatic Member Megalith's Avatar
    Join Date
    Oct 2006
    Location
    Secret location in the UK
    Posts
    879

    Re: DGV column ordering / reordering

    Hi jmcilhinney, sorry for the delay getting back as its been a hectic day, anyway here is my code
    Code:
                element = XElement.Parse(xml)
                InvList = _
                    ( _
                    From cust In element...<item> _
                    Order By CStr(cust...<category>.Value) Ascending, _
                             CInt(cust...<cost>.Value) Ascending _
                             Where CInt(cust...<num_owned>.Value) <> 0 _
                 Select New RowObject With _
                    { _
                      .ID = cust.<id>.Value, _
                      .Name = HttpUtility.UrlDecode(cust.<name>.Value), _
                      .PluralName = HttpUtility.UrlDecode(cust.<plural_name>.Value), _
                      .ImageUrl = HttpUtility.UrlDecode(cust.<image_url>.Value), _
                      .Details = HttpUtility.UrlDecode(cust.<details>.Value), _
                      .Cost = cust.<cost>.Value, _
                      .Quantity = cust.<num_owned>.Value, _
                      .Category = cust.<category>.Value, _
                      .SubCategory = cust.<sub_category>.Value _
                      } _
                    ).ToList
    the InvList is defined in my global module as
    Code:
    Friend InvList As New List(Of RowObject)
    If debugging is the process of removing bugs, then programming must be the process of putting them in.

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

    Re: DGV column ordering / reordering

    The reason that your columns are in the order that they're declared in the RowObject class is that you are binding a list of RowObject objects. The order you have the properties in your query is irrelevant because all you're doing there is setting properties of an object. The order you set properties in has no bearing whatsoever on the order that those properties will be retrieved.

    If you want to control the order of the properties using your query then you'll have to use an anonymous type, where you'll be defining the object at the same time as you set the property values.
    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

  5. #5

    Thread Starter
    Fanatic Member Megalith's Avatar
    Join Date
    Oct 2006
    Location
    Secret location in the UK
    Posts
    879

    Re: DGV column ordering / reordering

    Ah well i started this with an anonymous type and i ended up using this list for other methods within the app and so needed an object bound list. So i guess what you are saying is if i wish to populate a DGV with the contents of the list in a specific order then i need to change the property order within my object class (not the end of the world if i do do this)? i might however use a data repeater with this list instead as that gives me more control of the presentation anyway?
    If debugging is the process of removing bugs, then programming must be the process of putting them in.

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

    Re: DGV column ordering / reordering

    Quote Originally Posted by Megalith View Post
    So i guess what you are saying is if i wish to populate a DGV with the contents of the list in a specific order then i need to change the property order within my object class
    Nope; not saying that and wouldn't suggest that. You can't go changing a class definition every time you want to change the order of the columns in a grid.

    As I said in post #2, you can set the DisplayIndex of each column after they're created. If you're letting the grid create the columns automatically then that would be after binding. You can also create the columns yourself in the designer, so you can then set the column order in the designer.
    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

  7. #7

    Thread Starter
    Fanatic Member Megalith's Avatar
    Join Date
    Oct 2006
    Location
    Secret location in the UK
    Posts
    879

    Re: DGV column ordering / reordering

    would you know of a good resource for this or an example at all? kind of thought that having to reorder class definitions was a bad idea. which designer do you mean?
    If debugging is the process of removing bugs, then programming must be the process of putting them in.

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

    Re: DGV column ordering / reordering

    Quote Originally Posted by Megalith View Post
    which designer do you mean?
    The Windows Forms designer; the same one where you created the grid in the first place. It's just a control and you set a bunch of properties. The DataGridView is about as complex as controls get but the principles involved are the same as they are for very simple controls, so I wouldn't think a tutorial would be necessary. If you don't know what a property is for, look it up in the documentation, same as you would for a Label.
    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

  9. #9

    Thread Starter
    Fanatic Member Megalith's Avatar
    Join Date
    Oct 2006
    Location
    Secret location in the UK
    Posts
    879

    Re: DGV column ordering / reordering

    Quote Originally Posted by jmcilhinney View Post
    The Windows Forms designer; the same one where you created the grid in the first place.
    Ok thanks for the help, will look into the properties for it now. This project is heavy on data and i haven't used a DGV in a few years so a bit rusty. Will get back on this if i have any more issues with it. I seem to recall also i can control the width of columns. Need to have a play with it jog the mind
    If debugging is the process of removing bugs, then programming must be the process of putting them in.

  10. #10

    Thread Starter
    Fanatic Member Megalith's Avatar
    Join Date
    Oct 2006
    Location
    Secret location in the UK
    Posts
    879

    Re: DGV column ordering / reordering

    Thanks again got it now, one additional question tho but will start a new thread.
    If debugging is the process of removing bugs, then programming must be the process of putting them in.

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