This is a migrated thread and some comments may be shown as answers.

Replicate SelectedIndices for ListView

3 Answers 114 Views
ListView
This is a migrated thread and some comments may be shown as answers.
Joseph
Top achievements
Rank 1
Joseph asked on 14 Jan 2016, 06:32 PM

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.

 

 

3 Answers, 1 is accepted

Sort by
0
Accepted
KTK
Top achievements
Rank 1
answered on 15 Jan 2016, 09:47 AM

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!

0
Joseph
Top achievements
Rank 1
answered on 15 Jan 2016, 10:18 PM
KTK, thanks so much. Works perfectly. Wow, did not know that AddRange would also remove data from the source ListView. Much appreciated.
0
Hristo
Telerik team
answered on 18 Jan 2016, 02:41 PM
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
Tags
ListView
Asked by
Joseph
Top achievements
Rank 1
Answers by
KTK
Top achievements
Rank 1
Joseph
Top achievements
Rank 1
Hristo
Telerik team
Share this question
or