Nov 6th, 2005, 01:42 AM
#1
Thread Starter
Admodistrator
[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
Nov 6th, 2005, 02:00 AM
#2
Re: Listview, moving columns?
I'm confused. How can rows be between columns?
Nov 6th, 2005, 02:04 AM
#3
Thread Starter
Admodistrator
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
Nov 6th, 2005, 02:28 AM
#4
Re: Listview, moving columns?
Oh, OK, give me a couple of minutes.
Nov 6th, 2005, 02:41 AM
#5
Re: Listview, moving columns?
VB Code:
Dim lngIndex As Long
Dim strTemp(5) As String
For lngIndex = 1 To ListView1.ListItems.Count
strTemp(0) = ListView1.ListItems(lngIndex).SubItems(1)
strTemp(1) = ListView1.ListItems(lngIndex).SubItems(2)
strTemp(2) = ListView1.ListItems(lngIndex).SubItems(3)
strTemp(3) = ListView1.ListItems(lngIndex).SubItems(4)
strTemp(4) = ListView1.ListItems(lngIndex).SubItems(5)
strTemp(5) = ListView1.ListItems(lngIndex).SubItems(6)
ListView1.ListItems(lngIndex).SubItems(1) = strTemp(4)
ListView1.ListItems(lngIndex).SubItems(2) = strTemp(5)
ListView1.ListItems(lngIndex).SubItems(3) = strTemp(0)
ListView1.ListItems(lngIndex).SubItems(4) = strTemp(1)
ListView1.ListItems(lngIndex).SubItems(5) = strTemp(2)
ListView1.ListItems(lngIndex).SubItems(6) = strTemp(3)
Next
strTemp(0) = ListView1.ColumnHeaders(1).Text
strTemp(1) = ListView1.ColumnHeaders(2).Text
strTemp(2) = ListView1.ColumnHeaders(3).Text
strTemp(3) = ListView1.ColumnHeaders(4).Text
strTemp(4) = ListView1.ColumnHeaders(5).Text
strTemp(5) = ListView1.ColumnHeaders(6).Text
ListView1.ColumnHeaders(1).Text = strTemp(4)
ListView1.ColumnHeaders(2).Text = strTemp(5)
ListView1.ColumnHeaders(3).Text = strTemp(0)
ListView1.ColumnHeaders(4).Text = strTemp(1)
ListView1.ColumnHeaders(5).Text = strTemp(2)
ListView1.ColumnHeaders(6).Text = strTemp(3)
Nov 6th, 2005, 12:30 PM
#6
Thread Starter
Admodistrator
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.
Nov 6th, 2005, 12:33 PM
#7
Re: Listview, moving columns?
The For/Next loop moves (actually swaps) the data and the stuff after that swaps the headers.
Nov 6th, 2005, 12:39 PM
#8
Thread Starter
Admodistrator
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?
Nov 6th, 2005, 12:39 PM
#9
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:
Option Explicit
'Add a listview to your form
Private Sub Form_Load()
ListView1.AllowColumnReorder = True 'Allows drag n drop reordering of colums
ListView1.LabelEdit = lvwManual
ListView1.View = lvwReport
ListView1.ColumnHeaders.Add , , "Col 1"
ListView1.ColumnHeaders.Add , , "Col 2"
ListView1.ColumnHeaders.Add , , "Col 3"
ListView1.ColumnHeaders.Add , , "Col 4"
ListView1.ColumnHeaders.Add , , "Col 5"
ListView1.ColumnHeaders.Add , , "Col 6"
ListView1.ColumnHeaders.Add , , "Col 7"
ListView1.ListItems.Add , , "Test"
'Position is 1 based
ListView1.ColumnHeaders.Item(6).Position = 2
ListView1.ColumnHeaders.Item(7).Position = 3
End Sub
'RESULT COLUMN ORDERS:
'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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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
Nov 6th, 2005, 12:44 PM
#10
Thread Starter
Admodistrator
Re: Listview, moving columns?
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:
Option Explicit
'Add a listview to your form
Private Sub Form_Load()
ListView1.AllowColumnReorder = True 'Allows drag n drop reordering of colums
ListView1.LabelEdit = lvwManual
ListView1.View = lvwReport
ListView1.ColumnHeaders.Add , , "Col 1"
ListView1.ColumnHeaders.Add , , "Col 2"
ListView1.ColumnHeaders.Add , , "Col 3"
ListView1.ColumnHeaders.Add , , "Col 4"
ListView1.ColumnHeaders.Add , , "Col 5"
ListView1.ColumnHeaders.Add , , "Col 6"
ListView1.ColumnHeaders.Add , , "Col 7"
ListView1.ListItems.Add , , "Test"
'Position is 1 based
ListView1.ColumnHeaders.Item(6).Position = 2
ListView1.ColumnHeaders.Item(7).Position = 3
End Sub
'RESULT COLUMN ORDERS:
'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!
Nov 6th, 2005, 12:46 PM
#11
Re: Listview, moving columns?
I didn't realize there was a Position parameter for the headings, but how do you move the data?
Nov 6th, 2005, 12:48 PM
#12
Thread Starter
Admodistrator
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!
Nov 6th, 2005, 12:53 PM
#13
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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
Nov 6th, 2005, 12:54 PM
#14
Thread Starter
Admodistrator
Re: [RESOLVED] Listview, moving columns?
+2 to you
Nov 6th, 2005, 01:56 PM
#15
Re: [RESOLVED] Listview, moving columns?
I really don't understand. I tried the code that Rob posted and while the headings are moved the data isn't, so what is the point?
Nov 6th, 2005, 02:20 PM
#16
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:
Option Explicit
'Add a listview to your form
Private Sub Form_Load()
ListView1.AllowColumnReorder = True 'Allows drag n drop reordering of colums
ListView1.LabelEdit = lvwManual
ListView1.FullRowSelect = True
ListView1.View = lvwReport
ListView1.ColumnHeaders.Add , , "Col 1", 1000
ListView1.ColumnHeaders.Add , , "Col 2", 1000
ListView1.ColumnHeaders.Add , , "Col 3", 1000
ListView1.ColumnHeaders.Add , , "Col 4", 1000
ListView1.ColumnHeaders.Add , , "Col 5", 1000
ListView1.ColumnHeaders.Add , , "Col 6", 1000
ListView1.ColumnHeaders.Add , , "Col 7", 1000
ListView1.ListItems.Add , , "Col 1"
ListView1.ListItems(1).SubItems(1) = "Col 2"
ListView1.ListItems(1).SubItems(2) = "Col 3"
ListView1.ListItems(1).SubItems(3) = "Col 4"
ListView1.ListItems(1).SubItems(4) = "Col 5"
ListView1.ListItems(1).SubItems(5) = "Col 6"
ListView1.ListItems(1).SubItems(6) = "Col 7"
'Position is 1 based
'All the data is loaded into their respective columns 1 - 7
'Now switch the positions and see the data follown the colum.
ListView1.ColumnHeaders.Item(6).Position = 2
ListView1.ColumnHeaders.Item(7).Position = 3
End Sub
Attached Images
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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
Nov 6th, 2005, 03:16 PM
#17
Thread Starter
Admodistrator
Re: [RESOLVED] Listview, moving columns?
i used this on accident and it worked.. try it
lv1.ColumnHeaders(11).Position = 1
Nov 6th, 2005, 03:30 PM
#18
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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
Nov 6th, 2005, 04:10 PM
#19
Re: [RESOLVED] Listview, moving columns?
Rob, it's strange but when I run your code as is, it works. But if I move the two .Position lines to a command button it doesn't.
Nov 6th, 2005, 06:46 PM
#20
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:
Option Explicit
Private Sub cmdClose_Click()
Unload Me
End Sub
Private Sub cmdColLeft_Click()
Dim iCol1 As Integer
Dim iCol2 As Integer
Dim iCol3 As Integer
Dim iCol4 As Integer
Dim iCol5 As Integer
iCol1 = ListView1.ColumnHeaders(1).Position
iCol2 = ListView1.ColumnHeaders(2).Position
iCol3 = ListView1.ColumnHeaders(3).Position
iCol4 = ListView1.ColumnHeaders(4).Position
iCol5 = ListView1.ColumnHeaders(5).Position
If iCol1 = 1 Then
iCol1 = 5
Else
iCol1 = iCol1 - 1
End If
If iCol2 = 1 Then
iCol2 = 5
Else
iCol2 = iCol2 - 1
End If
If iCol3 = 1 Then
iCol3 = 5
Else
iCol3 = iCol3 - 1
End If
If iCol4 = 1 Then
iCol4 = 5
Else
iCol4 = iCol4 - 1
End If
If iCol5 = 1 Then
iCol5 = 5
Else
iCol5 = iCol5 - 1
End If
ListView1.ColumnHeaders(1).Position = iCol1
ListView1.ColumnHeaders(2).Position = iCol2
ListView1.ColumnHeaders(3).Position = iCol3
ListView1.ColumnHeaders(4).Position = iCol4
ListView1.ColumnHeaders(5).Position = iCol5
ListView1.Refresh
End Sub
Private Sub cmdColRight_Click()
Dim iCol1 As Integer
Dim iCol2 As Integer
Dim iCol3 As Integer
Dim iCol4 As Integer
Dim iCol5 As Integer
iCol1 = ListView1.ColumnHeaders(1).Position
iCol2 = ListView1.ColumnHeaders(2).Position
iCol3 = ListView1.ColumnHeaders(3).Position
iCol4 = ListView1.ColumnHeaders(4).Position
iCol5 = ListView1.ColumnHeaders(5).Position
If iCol1 = 5 Then
iCol1 = 1
Else
iCol1 = iCol1 + 1
End If
If iCol2 = 5 Then
iCol2 = 1
Else
iCol2 = iCol2 + 1
End If
If iCol3 = 5 Then
iCol3 = 1
Else
iCol3 = iCol3 + 1
End If
If iCol4 = 5 Then
iCol4 = 1
Else
iCol4 = iCol4 + 1
End If
If iCol5 = 5 Then
iCol5 = 1
Else
iCol5 = iCol5 + 1
End If
ListView1.ColumnHeaders(1).Position = iCol1
ListView1.ColumnHeaders(2).Position = iCol2
ListView1.ColumnHeaders(3).Position = iCol3
ListView1.ColumnHeaders(4).Position = iCol4
ListView1.ColumnHeaders(5).Position = iCol5
ListView1.Refresh
End Sub
Private Sub Form_Load()
Dim itmX As ListItem
Dim i As Integer
ListView1.AllowColumnReorder = True
ListView1.FullRowSelect = True
ListView1.LabelEdit = lvwManual
ListView1.MultiSelect = False
ListView1.View = lvwReport
For i = 1 To 5
ListView1.ColumnHeaders.Add , , "Column " & i
Next
For i = 1 To 10
Set itmX = ListView1.ListItems.Add(, , "Item 1")
itmX.SubItems(1) = "Item 2"
itmX.SubItems(2) = "Item 3"
itmX.SubItems(3) = "Item 4"
itmX.SubItems(4) = "Item 5"
Set itmX = Nothing
Next
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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
Nov 6th, 2005, 10:47 PM
#21
Re: [RESOLVED] Listview, moving columns?
Thanks, the Refresh is what was missing.
Posting Permissions
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
Forum Rules
Click Here to Expand Forum to Full Width