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

Programmatically selecting row/item

5 Answers 1207 Views
ListView
This is a migrated thread and some comments may be shown as answers.
Muhammad
Top achievements
Rank 1
Muhammad asked on 18 Dec 2015, 10:10 AM
How can I select a specific row programmatically... for example if my list have 10 items and on binding I want to select the 5th one.

5 Answers, 1 is accepted

Sort by
0
Pavel R. Pavlov
Telerik team
answered on 22 Dec 2015, 08:29 AM
Hi Muhammad,

With the currently released version of the RadListView there is no way to select items through code. The good new is that the requested feature is included in our source code. This means it will be available in our next official release. It will be available for download by the end of January 2016. 

Please stay tuned for the release and give it a try once it is out.

Regards,
Pavel R. Pavlov
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
0
Brent
Top achievements
Rank 1
answered on 16 May 2017, 12:53 PM
How is this done programmatically in code?
0
Stefan Nenchev
Telerik team
answered on 18 May 2017, 08:04 AM
Hello Brent,

As advised in the ticket you have raised, you can work with the SelectedItems collection of the RadListView and add a specific item from the ItemsSource collection of the control. For Example:

List<MyItem> items = new List<MyItem>();
for (int i = 0; i < 5; i++)
{
    items.Add(new MyItem() { Name = string.Format("Name {0}", i), ID = i });
}
this.listView.ItemsSource = items;
this.listView.SelectedItems.Add((this.listView.ItemsSource as List<MyItem>)[1]);


Regards,
Stefan Nenchev
Telerik by Progress
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
0
san
Top achievements
Rank 1
answered on 24 Jan 2018, 09:36 PM

hi I have some problem regarding SelectedItems.

<telerikDataControls:RadListView                
                  Grid.Row="1"
                  x:Name="listView" ItemsSource="{Binding Items}"
                  SelectionChanged="Handle_SelectionChanged"
                  SelectionMode="Multiple"/>

Here is my code behind.

protected override async void OnAppearing()
        {
            base.OnAppearing();
            IsBusy = true;
            await LoadData();
            IsBusy = false;
            //listView.ItemsSource = Items; // if this is not set, the SelectedItems.Add method throw exception
            if (selectedInstances != null && selectedInstances.Any())
            {
                {
                    foreach (var item in selectedInstances)
                        listView.SelectedItems.Add(item);
                }
            }
        }

 

private ObservableCollection<BaseListClass> _items;
        public ObservableCollection<BaseListClass> Items
        {
            get => _items;
            set
            {
                _items = value;
                OnPropertyChanged("Items");
            }
        }
 
void LoadData()
{
// Call To my web api to get the record in "list"
var list= GetDataFromServer();
Items = new ObservableCollection<BaseListClass>(list);
}

 

So how can i show selected items on page load for "Multiple" selection mode of ListView?

 

 

0
Lance | Manager Technical Support
Telerik team
answered on 25 Jan 2018, 06:39 PM
Hello San,

You haven't provided enough information, but it's likely due to you not setting your BindingContext correctly, thus the RadListView's ItemsSource is null.  I've written you a demo, attached.

For your convenience, here is the code: 

<ContentPage x:Name="MyPage" xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:telerikDataControls="clr-namespace:Telerik.XamarinForms.DataControls;assembly=Telerik.XamarinForms.DataControls"
             xmlns:listView="clr-namespace:Telerik.XamarinForms.DataControls.ListView;assembly=Telerik.XamarinForms.DataControls"
             x:Class="SelectedItemsOnPageLoad.Portable.StartPage">
     
    <Grid BindingContext="{x:Reference MyPage}">
        <telerikDataControls:RadListView x:Name="listView"
                               ItemsSource="{Binding Items}"
                               SelectionChanged="Handle_SelectionChanged"
                               SelectionMode="Multiple">
            <telerikDataControls:RadListView.ItemTemplate>
                <DataTemplate>
                    <listView:ListViewTextCell Text="{Binding Name}"/>
                </DataTemplate>
            </telerikDataControls:RadListView.ItemTemplate>
        </telerikDataControls:RadListView>
    </Grid>
</ContentPage>

namespace SelectedItemsOnPageLoad.Portable
{
    public partial class StartPage : ContentPage
    {
        private ObservableCollection<BaseListClass> _items;
        private ObservableCollection<BaseListClass> selectedInstances = new ObservableCollection<BaseListClass>();
 
        public StartPage()
        {
            InitializeComponent();
        }
 
        private void Handle_SelectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
             
        }
 
        protected override void OnAppearing()
        {
            base.OnAppearing();
 
            IsBusy = true;
 
            LoadData();
 
            IsBusy = false;
 
            foreach (var selectedInstance in selectedInstances)
            {
                var instanceInItemsList = Items.FirstOrDefault(item => item.Name == selectedInstance.Name);
 
                listView.SelectedItems.Add(instanceInItemsList);
            }
        }
         
        public ObservableCollection<BaseListClass> Items
        {
            get => _items ?? (_items = new ObservableCollection<BaseListClass>());
            set
            {
                _items = value;
                OnPropertyChanged();
            }
        }
         
        void LoadData()
        {
            var list = Enumerable.Range(1, 20).Select(i => new BaseListClass
            {
                Name = $"Item {i}"
            });
 
            foreach (var item in list)
            {
                Items.Add(item);
            }
 
            // adding some of the items to selected items collection
            selectedInstances.Add(Items[1]);
            selectedInstances.Add(Items[3]);
            selectedInstances.Add(Items[4]);
        }
    }
 
    public class BaseListClass
    {
        public string Name { get; set; }
    }
}

Note that I removed the await operator from the call on LoadData. Your shared code is not compilable as LoadData is not marked async. Additionally, you should never use async void unless it's top level event handler.


I've attached a runnable demo for you to see this in action at runtime:



Regards,
Lance | Tech Support Engineer, Sr.
Progress 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
Muhammad
Top achievements
Rank 1
Answers by
Pavel R. Pavlov
Telerik team
Brent
Top achievements
Rank 1
Stefan Nenchev
Telerik team
san
Top achievements
Rank 1
Lance | Manager Technical Support
Telerik team
Share this question
or