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

Programatically Select a DataBound item

3 Answers 402 Views
ListView
This is a migrated thread and some comments may be shown as answers.
Allen
Top achievements
Rank 1
Allen asked on 11 Sep 2012, 11:04 AM
how do i do this? the "Current" only highlights the item but not really selecting it. is there another way?

3 Answers, 1 is accepted

Sort by
0
Ivan Todorov
Telerik team
answered on 14 Sep 2012, 11:48 AM
Hi Rhea,

Thank you for your question.

To select an item programmatically, you can set the SelectedItem property of RadListView. Alternatively, you can use the Select(ListViewDataItem[] items) method:
//to select a single item:
this.radListView1.SelectedItem = this.radListView1.Items[0];
//to select multiple items when multiselect is enabled:
this.radListView1.Select(new ListViewDataItem[] { this.radListView1.Items[0], this.radListView1.Items[1] });

I hope this will help you. Feel free to write back if you have any further questions.

All the best,
Ivan Todorov
the Telerik team
RadControls for WinForms Q2'12 release is now live! Check out what's new or download a free trial >>
0
Oscar
Top achievements
Rank 2
answered on 21 Feb 2014, 05:42 PM
Good Day, 

I want to allow the item selection, but with only one click ( on click event, ) let the multiselection ( if I had CTRL key pressed ).
Is there some property for use selection with one click and simulate CTRL press?

Thanks a lot.
Oscar
0
Dimitar
Telerik team
answered on 26 Feb 2014, 12:44 PM
Hi Oscar,

Thank you for writing.

Currently, this functionality is not supported in RadListView. However it can be achieved by using the ItemMouseDown and ItemMouseUp events. For example you can store the selected items in the ItemMouseDown and restore them in ItemMouseUp event handler:
List<ListViewDataItem> selected = null;
 
void radListView1_ItemMouseDown(object sender, ListViewItemMouseEventArgs e)
{
    selected = radListView1.SelectedItems.ToList();
     
    if (!selected.Contains(e.Item))
    {
        selected.Add(e.Item);
    }
    else
    {
        selected.Remove(e.Item);
    }
}
 
void radListView1_ItemMouseUp(object sender, ListViewItemMouseEventArgs e)
{
     
    radListView1.SelectedItems.Clear();
    radListView1.Select(selected.ToArray());
}

If you have any questions, please do not hesitate to contact us.

Regards,
Dimitar
Telerik
Tags
ListView
Asked by
Allen
Top achievements
Rank 1
Answers by
Ivan Todorov
Telerik team
Oscar
Top achievements
Rank 2
Dimitar
Telerik team
Share this question
or