Results 1 to 21 of 21

Thread: [RESOLVED] Listview, moving columns?

  1. #1

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Resolved [RESOLVED] Listview, moving columns?

    Well, i cant really explain the title in just a few words. I want to add 2 more rows to my listview, but i dont want to add them at design time (alot of code to change then).

    The problem is, that i want the two rows that i add, i want to move them so they are positioned in-between two columns that are already there.
    So, i added rows 12,13 to my listview during design time. I want 12 and 13 to appear to be inbetween columns 1,2.

    Hope it makes sense

  2. #2

  3. #3

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Listview, moving columns?

    okay

    you have a listview with 7 columns during design time

    1|2|3|4|5|6|7

    during run time, you simply want to move the last two inbetween the first two:

    1|6|7|2|3|4|5

    get what i mean? i just want to change the order

  4. #4

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

    Re: Listview, moving columns?

    VB Code:
    1. Dim lngIndex As Long
    2. Dim strTemp(5) As String
    3.  
    4. For lngIndex = 1 To ListView1.ListItems.Count
    5.     strTemp(0) = ListView1.ListItems(lngIndex).SubItems(1)
    6.     strTemp(1) = ListView1.ListItems(lngIndex).SubItems(2)
    7.     strTemp(2) = ListView1.ListItems(lngIndex).SubItems(3)
    8.     strTemp(3) = ListView1.ListItems(lngIndex).SubItems(4)
    9.     strTemp(4) = ListView1.ListItems(lngIndex).SubItems(5)
    10.     strTemp(5) = ListView1.ListItems(lngIndex).SubItems(6)
    11.    
    12.     ListView1.ListItems(lngIndex).SubItems(1) = strTemp(4)
    13.     ListView1.ListItems(lngIndex).SubItems(2) = strTemp(5)
    14.     ListView1.ListItems(lngIndex).SubItems(3) = strTemp(0)
    15.     ListView1.ListItems(lngIndex).SubItems(4) = strTemp(1)
    16.     ListView1.ListItems(lngIndex).SubItems(5) = strTemp(2)
    17.     ListView1.ListItems(lngIndex).SubItems(6) = strTemp(3)
    18. Next
    19. strTemp(0) = ListView1.ColumnHeaders(1).Text
    20. strTemp(1) = ListView1.ColumnHeaders(2).Text
    21. strTemp(2) = ListView1.ColumnHeaders(3).Text
    22. strTemp(3) = ListView1.ColumnHeaders(4).Text
    23. strTemp(4) = ListView1.ColumnHeaders(5).Text
    24. strTemp(5) = ListView1.ColumnHeaders(6).Text
    25.  
    26. ListView1.ColumnHeaders(1).Text = strTemp(4)
    27. ListView1.ColumnHeaders(2).Text = strTemp(5)
    28. ListView1.ColumnHeaders(3).Text = strTemp(0)
    29. ListView1.ColumnHeaders(4).Text = strTemp(1)
    30. ListView1.ColumnHeaders(5).Text = strTemp(2)
    31. ListView1.ColumnHeaders(6).Text = strTemp(3)

  6. #6

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Listview, moving columns?

    marty, doesnt that simply change the text of the columnheaders? I need them to be moved, because i have so much code it will be a real pain to change.

  7. #7

  8. #8

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Listview, moving columns?

    Okay thanks marty, that is what i asked for. I need to explain better.

    By doing it your way, if i am continually adding to a listview then it will really mess up. I want to be able to access the two columns i used by there original numbers

    ie:
    1|6|7|2|3|4|5
    ^ to add something to this row, id like to be able to use lv1.listitems(i).listsubitems.add , , "stuff" and it will add to the 6 and 7th columns after 2345

    i could try and write up a project to explain if you like?

  9. #9
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Listview, moving columns?

    No need to re-order items like that when you can either do it by enabling Drag n Drop of columns for re-ordering or re-order via code, aka the .Position property.
    VB Code:
    1. Option Explicit
    2. 'Add a listview to your form
    3. Private Sub Form_Load()
    4.     ListView1.AllowColumnReorder = True 'Allows drag n drop reordering of colums
    5.     ListView1.LabelEdit = lvwManual
    6.     ListView1.View = lvwReport
    7.     ListView1.ColumnHeaders.Add , , "Col 1"
    8.     ListView1.ColumnHeaders.Add , , "Col 2"
    9.     ListView1.ColumnHeaders.Add , , "Col 3"
    10.     ListView1.ColumnHeaders.Add , , "Col 4"
    11.     ListView1.ColumnHeaders.Add , , "Col 5"
    12.     ListView1.ColumnHeaders.Add , , "Col 6"
    13.     ListView1.ColumnHeaders.Add , , "Col 7"
    14.     ListView1.ListItems.Add , , "Test"
    15.     'Position is 1 based
    16.     ListView1.ColumnHeaders.Item(6).Position = 2
    17.     ListView1.ColumnHeaders.Item(7).Position = 3
    18. End Sub
    19.  
    20.  
    21. 'RESULT COLUMN ORDERS:
    22. '1 | 6 | 7 | 2 | 3 | 4 | 5
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  10. #10

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Listview, moving columns?

    Quote Originally Posted by RobDog888
    No need to re-order items like that when you can either do it by enabling Drag n Drop of columns for re-ordering or re-order via code, aka the .Position property.
    VB Code:
    1. Option Explicit
    2. 'Add a listview to your form
    3. Private Sub Form_Load()
    4.     ListView1.AllowColumnReorder = True 'Allows drag n drop reordering of colums
    5.     ListView1.LabelEdit = lvwManual
    6.     ListView1.View = lvwReport
    7.     ListView1.ColumnHeaders.Add , , "Col 1"
    8.     ListView1.ColumnHeaders.Add , , "Col 2"
    9.     ListView1.ColumnHeaders.Add , , "Col 3"
    10.     ListView1.ColumnHeaders.Add , , "Col 4"
    11.     ListView1.ColumnHeaders.Add , , "Col 5"
    12.     ListView1.ColumnHeaders.Add , , "Col 6"
    13.     ListView1.ColumnHeaders.Add , , "Col 7"
    14.     ListView1.ListItems.Add , , "Test"
    15.     'Position is 1 based
    16.     ListView1.ColumnHeaders.Item(6).Position = 2
    17.     ListView1.ColumnHeaders.Item(7).Position = 3
    18. End Sub
    19.  
    20.  
    21. 'RESULT COLUMN ORDERS:
    22. '1 | 6 | 7 | 2 | 3 | 4 | 5
    Aha! What i was looking for! Thankyou robdog, and marty, sorry for not explaining well


    That was alot easier than i expected!

  11. #11

  12. #12

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: [RESOLVED] Listview, moving columns?

    @marty, you dont. You still add to the same listview row and everything else. It just appears as if its the 2nd item!

  13. #13
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: [RESOLVED] Listview, moving columns?

    Yes, you got it right remix. The .Index is read only at runtime but you can make the appearance of where the columns are located vary depending on your needs.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  14. #14

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: [RESOLVED] Listview, moving columns?

    +2 to you

  15. #15

  16. #16
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: [RESOLVED] Listview, moving columns?

    Here you go Martin. I wrote an example for you to test with.
    Also, try draging and dropping the columns around. You will see the data follows.




    VB Code:
    1. Option Explicit
    2. 'Add a listview to your form
    3. Private Sub Form_Load()
    4.     ListView1.AllowColumnReorder = True 'Allows drag n drop reordering of colums
    5.     ListView1.LabelEdit = lvwManual
    6.     ListView1.FullRowSelect = True
    7.     ListView1.View = lvwReport
    8.     ListView1.ColumnHeaders.Add , , "Col 1", 1000
    9.     ListView1.ColumnHeaders.Add , , "Col 2", 1000
    10.     ListView1.ColumnHeaders.Add , , "Col 3", 1000
    11.     ListView1.ColumnHeaders.Add , , "Col 4", 1000
    12.     ListView1.ColumnHeaders.Add , , "Col 5", 1000
    13.     ListView1.ColumnHeaders.Add , , "Col 6", 1000
    14.     ListView1.ColumnHeaders.Add , , "Col 7", 1000
    15.     ListView1.ListItems.Add , , "Col 1"
    16.     ListView1.ListItems(1).SubItems(1) = "Col 2"
    17.     ListView1.ListItems(1).SubItems(2) = "Col 3"
    18.     ListView1.ListItems(1).SubItems(3) = "Col 4"
    19.     ListView1.ListItems(1).SubItems(4) = "Col 5"
    20.     ListView1.ListItems(1).SubItems(5) = "Col 6"
    21.     ListView1.ListItems(1).SubItems(6) = "Col 7"
    22.     'Position is 1 based
    23.     'All the data is loaded into their respective columns 1 - 7
    24.     'Now switch the positions and see the data follown the colum.
    25.     ListView1.ColumnHeaders.Item(6).Position = 2
    26.     ListView1.ColumnHeaders.Item(7).Position = 3
    27. End Sub
    Attached Images Attached Images  
    Attached Files Attached Files
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  17. #17

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: [RESOLVED] Listview, moving columns?

    i used this on accident and it worked.. try it
    lv1.ColumnHeaders(11).Position = 1

  18. #18
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: [RESOLVED] Listview, moving columns?

    Same thing as the .Item as the Item is the default property for the ColumnHeaders collection.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  19. #19

  20. #20
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: [RESOLVED] Listview, moving columns?

    Hmm, works for me. Try this example of shifting column 1 either to the left or right depending on the button you click. Did you add a .Refresh?
    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub cmdClose_Click()
    4.     Unload Me
    5. End Sub
    6.  
    7. Private Sub cmdColLeft_Click()
    8.  
    9.     Dim iCol1 As Integer
    10.     Dim iCol2 As Integer
    11.     Dim iCol3 As Integer
    12.     Dim iCol4 As Integer
    13.     Dim iCol5 As Integer
    14.    
    15.     iCol1 = ListView1.ColumnHeaders(1).Position
    16.     iCol2 = ListView1.ColumnHeaders(2).Position
    17.     iCol3 = ListView1.ColumnHeaders(3).Position
    18.     iCol4 = ListView1.ColumnHeaders(4).Position
    19.     iCol5 = ListView1.ColumnHeaders(5).Position
    20.    
    21.     If iCol1 = 1 Then
    22.         iCol1 = 5
    23.     Else
    24.         iCol1 = iCol1 - 1
    25.     End If
    26.     If iCol2 = 1 Then
    27.         iCol2 = 5
    28.     Else
    29.         iCol2 = iCol2 - 1
    30.     End If
    31.     If iCol3 = 1 Then
    32.         iCol3 = 5
    33.     Else
    34.         iCol3 = iCol3 - 1
    35.     End If
    36.     If iCol4 = 1 Then
    37.         iCol4 = 5
    38.     Else
    39.         iCol4 = iCol4 - 1
    40.     End If
    41.     If iCol5 = 1 Then
    42.         iCol5 = 5
    43.     Else
    44.         iCol5 = iCol5 - 1
    45.     End If
    46.    
    47.     ListView1.ColumnHeaders(1).Position = iCol1
    48.     ListView1.ColumnHeaders(2).Position = iCol2
    49.     ListView1.ColumnHeaders(3).Position = iCol3
    50.     ListView1.ColumnHeaders(4).Position = iCol4
    51.     ListView1.ColumnHeaders(5).Position = iCol5
    52.    
    53.     ListView1.Refresh
    54.    
    55. End Sub
    56.  
    57. Private Sub cmdColRight_Click()
    58.    
    59.     Dim iCol1 As Integer
    60.     Dim iCol2 As Integer
    61.     Dim iCol3 As Integer
    62.     Dim iCol4 As Integer
    63.     Dim iCol5 As Integer
    64.    
    65.     iCol1 = ListView1.ColumnHeaders(1).Position
    66.     iCol2 = ListView1.ColumnHeaders(2).Position
    67.     iCol3 = ListView1.ColumnHeaders(3).Position
    68.     iCol4 = ListView1.ColumnHeaders(4).Position
    69.     iCol5 = ListView1.ColumnHeaders(5).Position
    70.    
    71.     If iCol1 = 5 Then
    72.         iCol1 = 1
    73.     Else
    74.         iCol1 = iCol1 + 1
    75.     End If
    76.     If iCol2 = 5 Then
    77.         iCol2 = 1
    78.     Else
    79.         iCol2 = iCol2 + 1
    80.     End If
    81.     If iCol3 = 5 Then
    82.         iCol3 = 1
    83.     Else
    84.         iCol3 = iCol3 + 1
    85.     End If
    86.     If iCol4 = 5 Then
    87.         iCol4 = 1
    88.     Else
    89.         iCol4 = iCol4 + 1
    90.     End If
    91.     If iCol5 = 5 Then
    92.         iCol5 = 1
    93.     Else
    94.         iCol5 = iCol5 + 1
    95.     End If
    96.    
    97.     ListView1.ColumnHeaders(1).Position = iCol1
    98.     ListView1.ColumnHeaders(2).Position = iCol2
    99.     ListView1.ColumnHeaders(3).Position = iCol3
    100.     ListView1.ColumnHeaders(4).Position = iCol4
    101.     ListView1.ColumnHeaders(5).Position = iCol5
    102.    
    103.     ListView1.Refresh
    104.    
    105. End Sub
    106.  
    107. Private Sub Form_Load()
    108.    
    109.     Dim itmX As ListItem
    110.     Dim i As Integer
    111.    
    112.     ListView1.AllowColumnReorder = True
    113.     ListView1.FullRowSelect = True
    114.     ListView1.LabelEdit = lvwManual
    115.     ListView1.MultiSelect = False
    116.     ListView1.View = lvwReport
    117.    
    118.     For i = 1 To 5
    119.         ListView1.ColumnHeaders.Add , , "Column " & i
    120.     Next
    121.     For i = 1 To 10
    122.         Set itmX = ListView1.ListItems.Add(, , "Item 1")
    123.         itmX.SubItems(1) = "Item 2"
    124.         itmX.SubItems(2) = "Item 3"
    125.         itmX.SubItems(3) = "Item 4"
    126.         itmX.SubItems(4) = "Item 5"
    127.         Set itmX = Nothing
    128.     Next
    129.    
    130. End Sub
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  21. #21

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