Programmatically selecting row/item

6 posts, 0 answers
  1. Muhammad
    Muhammad avatar
    7 posts
    Member since:
    May 2014

    Posted 18 Dec 2015 Link to this post

    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.
  2. Pavel R. Pavlov
    Admin
    Pavel R. Pavlov avatar
    1251 posts

    Posted 22 Dec 2015 Link to this post

    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
  3. Brent
    Brent avatar
    3 posts
    Member since:
    Jun 2011

    Posted 16 May 2017 Link to this post

    How is this done programmatically in code?
  4. Stefan Nenchev
    Admin
    Stefan Nenchev avatar
    665 posts

    Posted 18 May 2017 Link to this post

    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
  5. san
    san avatar
    2 posts
    Member since:
    Aug 2012

    Posted 24 Jan 2018 in reply to Stefan Nenchev Link to this post

    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?

     

     

  6. Lance | Manager Technical Support
    Admin
    Lance | Manager Technical Support avatar
    1193 posts

    Posted 25 Jan 2018 Link to this post

    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
Back to Top