-
[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
-
Re: Listview, moving columns?
I'm confused. How can rows be between columns?
-
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
-
Re: Listview, moving columns?
Oh, OK, give me a couple of minutes.
-
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)
-
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.
-
Re: Listview, moving columns?
The For/Next loop moves (actually swaps) the data and the stuff after that swaps the headers.
-
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?
-
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
-
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:
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 :sick:
That was alot easier than i expected!
-
Re: Listview, moving columns?
I didn't realize there was a Position parameter for the headings, but how do you move the data?
-
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!
-
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.
-
Re: [RESOLVED] Listview, moving columns?
-
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?
-
2 Attachment(s)
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.
http://www.vbforums.com/attachment.p...chmentid=42414
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
-
Re: [RESOLVED] Listview, moving columns?
i used this on accident and it worked.. try it
lv1.ColumnHeaders(11).Position = 1
-
Re: [RESOLVED] Listview, moving columns?
Same thing as the .Item as the Item is the default property for the ColumnHeaders collection. ;)
-
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.
-
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
-
Re: [RESOLVED] Listview, moving columns?
Thanks, the Refresh is what was missing.