Replicate SelectedIndices for ListView

4 posts, 1 answers
  1. Joseph
    Joseph avatar
    4 posts
    Member since:
    Mar 2013

    Posted 14 Jan Link to this post

    I am attempting to move multiple selected items between Telerik ListView controls. If I was doing this with a Winforms Listbox, it would look like this:

    private void button1_Click(object sender, EventArgs e)
    {
        for(int x = listBox1.SelectedIndices.Count - 1; x>= 0; x--)
        {
            int idx = listBox1.SelectedIndices[x];
            listBox2.Items.Add(listBox1.Items[idx]);
            listBox1.Items.RemoveAt(idx);
        }
    }
    However, SelectedIndices does not seem to be available. I've tried to use SelectedItems, but my attempts either yield exceptions (because the index was changed at the first delete) or incorrectly listed items in the target list (again because of previous item removals). How does one do this with two Telerik ListView controls? Thanks.

     

     

  2. Answer
    KTK
    KTK avatar
    11 posts
    Member since:
    Oct 2013

    Posted 15 Jan in reply to Joseph Link to this post

    Hello Joseph.

    I ran a quick test project with two RadListViews and one RadButton that tries to move selected items from first RadListView to the seconde one.

    Try this code:

    private void RadForm1_Load(object sender, EventArgs e)
    {
        for (int i = 0; i < 10; i++)
        {
            this.radListView1.Items.Add(i);
        }
        this.radListView1.MultiSelect = true;
    }
     
    private void radButton1_Click(object sender, EventArgs e)
    {
        ListViewSelectedItemCollection selected = this.radListView1.SelectedItems;
        this.radListView2.Items.AddRange(selected.ToArray());
    }

    It seems (int my test project) that there is no need to explicitely remove the data from the first list.

    See attached images.

    I used Telerik v.2016.1.112.40

    Hope it helps!

  3. UI for WinForms is Visual Studio 2017 Ready
  4. Joseph
    Joseph avatar
    4 posts
    Member since:
    Mar 2013

    Posted 15 Jan in reply to KTK Link to this post

    KTK, thanks so much. Works perfectly. Wow, did not know that AddRange would also remove data from the source ListView. Much appreciated.
  5. Hristo Merdjanov
    Admin
    Hristo Merdjanov avatar
    712 posts

    Posted 18 Jan Link to this post

    Hello guys,

    Thank you for writing.

    KTK, thank you for helping Joseph and sharing your solution. I confirm that this is a valid approach.

    What is happening behind the scenes is that before adding each of the ListViewDataItems to the second RadListView control, it is being checked for its Owner. If it has an owner the object gets removed from its 
    Items collection. The same is also valid if you pass each of the ListViewDataItems individually: 
    private void radButton1_Click(object sender, EventArgs e)
    {
        ListViewDataItem[] selected = this.radListView1.SelectedItems.ToArray();
        for (int i = 0; i < selected.Length; i++)
        {
            this.radListView2.Items.Add(selected[i]);
        }
    }

    I hope this helps. Should you have further questions please do not hesitate to write back.

    Regards,
    Hristo Merdjanov
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Back to Top