Hello,

it's possible to move listview1 subitem(1) to another listview2 subitem(5)

please help

Recommended Answers

All 5 Replies

You could just try yourself if it can be done...

Anyway, here's my test:

Dim newItem As ListViewItem

' Create sub item
newItem = New ListViewItem
newItem.SubItems.Add("first")
' Add one lv-item with one sub item
ListView1.Items.Add(newItem)

' Show the value
MessageBox.Show("ListView1[0,1] = " & ListView1.Items(0).SubItems(1).ToString())

' Second listview
' Create sub items
newItem = New ListViewItem
newItem.SubItems.Add("first")
newItem.SubItems.Add("second")
newItem.SubItems.Add("third")
newItem.SubItems.Add("fourth")
newItem.SubItems.Add("fifth")
' Add one lv-item with five sub item
ListView2.Items.Add(newItem)

' Assign
ListView1.Items(0).SubItems(1) = ListView2.Items(0).SubItems(5)

' Show the value
MessageBox.Show("ListView1[0,1] = " & ListView1.Items(0).SubItems(1).ToString())

Sub item wasn't moved, it was copied but that shouldn't make any difference :)

HTH

Hi, Teme64 thanks for ur reply, but all i want is to transfer column1 to another column sorry my title is not appropriate to my question


LISTVIEW1
column 1 | column 2 | column3 | column4 | column 5
Apple
Orange
Mango
Guava
Grape

LISTVIEW2
column1 | column 2 | column3 | column4 | column5
_________________________________Apple
_________________________________Orange
_________________________________Mango
_________________________________Guava
_________________________________Grape

Listview control doesn't have any column property so you can't copy "subitem column" directly. Instead you have to copy (move) sub items in a loop one by one:

Dim newItem As ListViewItem

' Create sub item
newItem = New ListViewItem
newItem.SubItems.Add("LV1 first (row1)")
' Add one lv-item with one sub item
ListView1.Items.Add(newItem)
' Create sub item
newItem = New ListViewItem
newItem.SubItems.Add("LV1 first (row2)")
' Add one lv-item with one sub item
ListView1.Items.Add(newItem)
' Create sub item
newItem = New ListViewItem
newItem.SubItems.Add("LV1 first (row3)")
' Add one lv-item with one sub item
ListView1.Items.Add(newItem)

' Second listview
' Create sub items
newItem = New ListViewItem
newItem.SubItems.Add("first")
newItem.SubItems.Add("second")
newItem.SubItems.Add("third")
newItem.SubItems.Add("fourth")
newItem.SubItems.Add("fifth")
' Add one lv-item with five sub item
ListView2.Items.Add(newItem)

' Check that the target listview has at least as many rows as will be copied!
If ListView2.Items.Count < ListView1.Items.Count - 1 Then
    For i As Integer = ListView2.Items.Count To ListView1.Items.Count - 1
        ' Add rows
        newItem = New ListViewItem
        newItem.SubItems.AddRange(New String() {"", "", "", "", ""})
        ListView2.Items.Add(newItem)
    Next i
End If

' Move
Dim rowIX As Integer = 0

For rowIX = 0 To ListView1.Items.Count - 1
    ListView2.Items(rowIX).SubItems(5) = ListView1.Items(rowIX).SubItems(1)
Next rowIX

' Show the copied value
For rowIX = 0 To ListView2.Items.Count - 1
    MessageBox.Show("ListView2[" & rowIX & ",5] = " & ListView2.Items(rowIX).SubItems(5).ToString)
Next rowIX

In this case sub items are copied from LV1 to LV2 as opposite to previous code. Only thing you have to remember is that the target LV has to have enough rows. I added the rows before the copy loop but loops can be combined too.

HTH

your so great, thanks for your effort Teme64, i really appreciated it... i try to rebuild your code but it seems not working... i think your code is correct but for now i'm trying to study the property of listview..

here's my sample i have already copy the item in subitem(0) to subitem(1)
but i don't know how to transfer to another listview...

For i As Integer = 0 To ListView1.Items.Count - 1

            Dim item As ListViewItem = ListView1.Items(i)

            item.SubItems.Add(1).Text = item.Text

        Next

I should have explained my code better.

Lines 1-28 fill ListView1 and ListView2 with test data, so no need to take them in to account.

It's important that the target listview has at least as many rows as the source listview when you copy sub items. Lines 30-38 add empty rows to the target (ListView2) if needed. This could be checked and done in the copy loop too.

Copying is done in the lines 40-45. Actually all you need is the for loop in lines 43-45 (assuming the target listview has enough rows).

Lines 47-50 dump the content from ListView2 and this is also just for debugging the code.

I hope this made things clearer. As you saw above the copying sub items is very simple operation, just three lines of code.

HTH

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.