Results 1 to 10 of 10

Thread: Listview Column ReOrder

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2010
    Posts
    4

    Listview Column ReOrder

    I have several listviews in my VB6 application that are filled with data from an Access database. I have the "AllowColumnHeaderReorder" value set to true. This allows me to change the arrangement/order of the Columns and display the information, but it does not save this order for future views. Each time a new set of data is displayed, I have to rearrange the columns again.

    Is there sample source code available that would allow a user to select what columns are displayed, in a listview, and in what order, and have those selections saved to an ini file, or someplace else?

    The VB6 "Reference Listview" is an example of what I'd like to use to set these display settings. Then when the listview loads the data, it will be in the correct order.

    Thank you in advance.
    Attached Images Attached Images  

  2. #2

  3. #3
    Fanatic Member
    Join Date
    Mar 2009
    Posts
    804

    Re: Listview Column ReOrder

    In addition, have a look at the ColumnHeader.Position and
    ColumnHeader.SubItemIndex properties

  4. #4

    Thread Starter
    New Member
    Join Date
    Jan 2010
    Posts
    4

    Re: Listview Column ReOrder

    Hi Mr. M.,

    Thank you for you response. But I was hoping for a reply with some code I could use as a beginning. Like your link, "VB - Color a row in a ListView".

    With some source to examine, that covers how the form functions that I included in the attached illustration, I might be able to work out a solution for my needs.

    Thank you again.

  5. #5

  6. #6

    Thread Starter
    New Member
    Join Date
    Jan 2010
    Posts
    4

    Re: Listview Column ReOrder

    Thank you Marty. This gives me a starting point. I've already added the Northwind database and I think I understand the basics.

  7. #7
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: Listview Column ReOrder

    I forgot to mention that all my changes are marked with 'NEW. Don't hesitate to ask if you have questions. When you have the answer you need in this thread you can help us by marking the thread as resolved. If you have JavaScript enabled you can do that by selecting the Mark Thread Resolved item from the Thread Tools menu. Otherwise please insert "[Resolved]" at the start of the Subject and select the green checkmark from the Post Icons. Also if someone has been particularly helpful you have the ability to affect their forum "reputation" by rating their post. Only those ratings that you give after you have 20 posts will actually count, but in all cases the person you rate will see your rating and know that you appreciate their help. BTW, no need to do it for me.

  8. #8

    Thread Starter
    New Member
    Join Date
    Jan 2010
    Posts
    4

    Re: Listview Column ReOrder

    Hi Marty,

    The information and code was a great start. I found an issue with the unload event, which is probably why you weren't happy with it. The code works fine the first time to save the re-ordered columns. But when you close the application the second time, the order reverts back to 123.

    I know why this is happening, and I'm on the job of creating a solution. At least I have the basics to work with now. When I have something that works, I'll post it.

    Private Sub Form_Unload(Cancel As Integer)

    'NEW - Save the column order. (I'm not happy with this but it works.)
    Dim strOrder As String
    Dim intVal As Integer

    intVal = 1
    Do Until lvFind.ColumnHeaders(intVal).Position = 1
    intVal = intVal + 1
    Loop
    strOrder = lvFind.ColumnHeaders(intVal).Index

    intVal = 1
    Do Until lvFind.ColumnHeaders(intVal).Position = 2
    intVal = intVal + 1
    Loop
    strOrder = strOrder & lvFind.ColumnHeaders(intVal).Index

    intVal = 1
    Do Until lvFind.ColumnHeaders(intVal).Position = 3
    intVal = intVal + 1
    Loop
    strOrder = strOrder & lvFind.ColumnHeaders(intVal).Index
    SaveSetting "MyColumnPositionApp", "Column Positions", "MyListview", strOrder


    Set frmFind = Nothing

    End Sub

  9. #9
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: Listview Column ReOrder

    Okay I've got it.

    Code:
    Private Sub Form_Load()
        
        ' Create the instance of the database class
        Set gDB = New CDatabase
        
        ' Open the database
        gDB.dbName = App.Path & "\northwind.MDB"
        
        
       'NEW - Get saved order. If there are none (1st run) then the default order is 123
       mstrColumnPositions = GetSetting("MyColumnPositionApp", _
                            "Column Positions", "MyListview", "Product|Supplier ID|Category ID")
        mstrColumnPositions = Replace(mstrColumnPositions, " ", "_")
       
        Dim strParts() As String
        strParts = Split(mstrColumnPositions, "|")
        
        CallByName frmFind, strParts(0), VbMethod
        CallByName frmFind, strParts(1), VbMethod
        CallByName frmFind, strParts(2), VbMethod
    
       lvFind.BorderStyle = ccFixedSingle ' Set BorderStyle property.
       lvFind.View = lvwReport ' Set View property to Report.
    
       ' Label OptionButton controls with SortOrder options.
          OptSort(0).Caption = "Sort Ascending"
          OptSort(1).Caption = "Sort Descending"
          lvFind.SortOrder = lvwAscending ' Sort ascending.
          OptSort(0).Value = True
    
    End Sub
    Replace the Column1, etc subs with these.
    Code:
    Public Sub Product()
       Dim clmX As ColumnHeader
       Set clmX = lvFind.ColumnHeaders.Add(, , "Product", (lvFind.Width - 1066) / 2)
    
    End Sub
    
    Public Sub Supplier_ID()
       Dim clmX As ColumnHeader
       Set clmX = lvFind.ColumnHeaders.Add(, , "Supplier ID", (lvFind.Width - 1066) / 2)
    
    End Sub
    
    Public Sub Category_ID()
       Dim clmX As ColumnHeader
       Set clmX = lvFind.ColumnHeaders.Add(, , "Category ID", 976)
    
    End Sub
    Code:
    Private Sub Form_Unload(Cancel As Integer)
        
        Dim strOrder As String
        Dim intVal As Integer
        
        SaveSetting "MyColumnPositionApp", "Column Positions", "MyListview", _
                     lvFind.ColumnHeaders(lvFind.ColumnHeaders(1).Position).Text & "|" & _
                     lvFind.ColumnHeaders(lvFind.ColumnHeaders(2).Position).Text & "|" & _
                     lvFind.ColumnHeaders(lvFind.ColumnHeaders(3).Position).Text
    
                   
        Set frmFind = Nothing
        
    End Sub

  10. #10
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: Listview Column ReOrder

    BTW, your registry will contain 123 or some variation of that order and in my new scheme you use the column header text so you'll have to override what is there the first time after you make these above changes. Just in case you don't know how to do that

    1. Put a breakpoint on the mstrColumnPositions = Replace(mstrColumnPositions, " ", "_") line
    2. In the Immediate Window type mstrColumnPositions = "Product|Supplier ID|Category ID" and press return.

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