add items and reorder problem

2 posts, 1 answers
  1. Florian
    Florian avatar
    3 posts
    Member since:
    Jul 2012

    Posted 30 Jul 2012 Link to this post

    Hi

    I have ListView in "detailsView" Mode with two columns.

    Now I want to add Entries to this list.
    Dim lvItem1 As New ListViewDataItem()
    lvItem1.Item(0) = "CL1"
    lvItem1.Item(1) = "1000"
    lvItem1.Tag = New clsFoundationPlane("CL1", 1000)
    Me.lvHorValues.Items.Add(lvItem1)
     
    Dim lvItem2 As New ListViewDataItem()
    lvItem2.Item(0) = "CL2"
    lvItem2.Item(1) = "2000"
    lvItem2.Tag = New clsFoundationPlane("CL2", 2000)
    Me.lvHorValues.Items.Add(lvItem2)

    Now I have two lines in my ListView but with no text in it. What have I done wrong?

    Second question:
    How can I reorder my entries. I have two buttons with "Up" and "Down". So that the selected item can be moved in the list upd and down.

    Thanks Flo
  2. Answer
    Ivan Todorov
    Admin
    Ivan Todorov avatar
    688 posts

    Posted 31 Jul 2012 Link to this post

    Hello Florian,

    Thank you for writing.

    The cell text of your items is not shown because you are setting it before they are added to the RadListView and they are not aware of the columns that were added to the list view. To overcome this, you can either add the list items before you set their text:
    Dim lvItem1 As New ListViewDataItem()
    Me.RadListView1.Items.Add(lvItem1)
    lvItem1.Item(0) = "CL1"
    lvItem1.Item(1) = "1000"

    or use the SubItems collection:
    Dim lvItem2 As New ListViewDataItem()
    lvItem2.SubItems.Add("CL2")
    lvItem2.SubItems.Add("2000")
    Me.RadListView1.Items.Add(lvItem2)

    The SubItems is a collection that is used to store design-time serialized data but can be used to hold initial values.

    As to your second question, the following code demonstrates a sample implementation of Up and Down buttons:
    Private Sub RadButtonUp_Click(sender As System.Object, e As System.EventArgs) Handles RadButtonUp.Click
        Dim selectedItem As ListViewDataItem = Me.RadListView1.SelectedItem
        If Not selectedItem Is Nothing Then
            Dim index As Integer = Me.RadListView1.Items.IndexOf(selectedItem)
            index -= 1
            If index < 0 Then
                index = 0
            End If
            Me.RadListView1.Items.Remove(selectedItem)
            Me.RadListView1.Items.Insert(index, selectedItem)
        End If
    End Sub
     
    Private Sub RadButtonDown_Click(sender As System.Object, e As System.EventArgs) Handles RadButtonDown.Click
        Dim selectedItem As ListViewDataItem = Me.RadListView1.SelectedItem
        If Not selectedItem Is Nothing Then
            Dim index As Integer = Me.RadListView1.Items.IndexOf(selectedItem)
            index += 1
            If index >= Me.RadListView1.Items.Count Then
                index = Me.RadListView1.Items.Count - 1
            End If
            Me.RadListView1.Items.Remove(selectedItem)
            Me.RadListView1.Items.Insert(index, selectedItem)
        End If
    End Sub

    I hope you find this useful. Do not hesitate to write back if you have any additional questions.

    Greetings,
    Ivan Todorov
    the Telerik team
    RadControls for WinForms Q2'12 release is now live! Check out what's new or download a free trial >>
  3. UI for WinForms is Visual Studio 2017 Ready
Back to Top