Programmatically Selecting Items by Value in Listbox

12 posts, 1 answers
  1. Armysniper
    Armysniper avatar
    61 posts
    Member since:
    Mar 2005

    Posted 16 Feb 2011 Link to this post

    I have a RadListBox control and I want to select items based on the value of the item (which is stored in my database). How can I accomplish this with the control?
  2. Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 16 Feb 2011 Link to this post

    Hello,

    In order to select the value, you just need to set the selected value property.
    Please consider the following code

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using Telerik.WinControls;
    using Telerik.WinControls.UI;
      
      
    namespace RadListBox_C
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
      
            private List<Car> myList = new List<Car>();
      
            private void Form1_Load(object sender, EventArgs e)
            {
      
                this.radListControl1.SelectionMode = SelectionMode.One;
                myList.Add(new Car("BMW", 1));
                myList.Add(new Car("Audi", 2));
                myList.Add(new Car("Merc", 3));
                myList.Add(new Car("VW", 4));
                myList.Add(new Car("Ford", 5));
      
      
                this.radListControl1.SelectedItem = null;
                this.radListControl1.DataSource = myList;
                this.radListControl1.DisplayMember = "Make";
                this.radListControl1.ValueMember = "Id";
      
                this.radListControl1.SelectedValue = 2; // Audi
      
            }
        }
      
        public class Car
        {
            public Car(string make, int id)
            {
                this.Make = make;
                this.Id = id;
            }
      
            public string Make
            {get; set;}
      
            public int Id 
            {get; set;}
        }
      
    }

    Hope that helps
    Richard
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Armysniper
    Armysniper avatar
    61 posts
    Member since:
    Mar 2005

    Posted 16 Feb 2011 Link to this post

    I should have been clearer. I need to select mutliple items, not a single item. I know there is a SelectedItems collection but how do I use it?
  5. Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 16 Feb 2011 Link to this post

    Hi,

    There are other ways, but this is the simplest I beleive.

    this.radListControl1.SelectionMode = SelectionMode.MultiSimple;
    myList.Add(new Car("BMW", 1));
    myList.Add(new Car("Audi", 2));
    myList.Add(new Car("Merc", 3));
    myList.Add(new Car("VW", 4));
    myList.Add(new Car("Ford", 5));
    this.radListControl1.DataSource = myList;
    this.radListControl1.DisplayMember = "Make";
    this.radListControl1.ValueMember = "Id";
    this.radListControl1.SelectedIndex = -1; // clear
    this.radListControl1.SelectedValue = 1;
    this.radListControl1.SelectedValue = 3;

    Hope that helps
    Richard
  6. Armysniper
    Armysniper avatar
    61 posts
    Member since:
    Mar 2005

    Posted 17 Feb 2011 Link to this post

    That does not work. That property only works for single selection mode. I need to have multiple selections at the same time. Anyone else from Telerik want to jump in on this one? I dont want to have to rip this out for a new one. I am using the 2009 version right now because the 2010 version of the controls horked up my project.
  7. Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 17 Feb 2011 Link to this post

    Hello,

    The solution that I provided works correctly for MultiSimple ListBox. For MultiExtended you could use the following

    //this.radListControl1.SelectionMode = SelectionMode.MultiSimple;
    this.radListControl1.SelectionMode = SelectionMode.MultiExtended;
    myList.Add(new Car("BMW", 1));
    myList.Add(new Car("Audi", 2));
    myList.Add(new Car("Merc", 3));
    myList.Add(new Car("VW", 4));
    myList.Add(new Car("Ford", 5));
    this.radListControl1.DataSource = myList;
    this.radListControl1.DisplayMember = "Make";
    this.radListControl1.ValueMember = "Id";
    List<int> selected = new List<int>();
    selected.Add(1);
    selected.Add(3);
    this.radListControl1.SelectedIndex = -1; // clear
    foreach (RadListDataItem item in this.radListControl1.Items)
    {
        if (selected.Contains(Convert.ToInt32(item.Value)))
        {
            item.Selected = true;
        }
    }

    Note that this is tested with 2010 Q3 SP1 (the latest version). even though I think this will work for you, I'd advise to upgrade as there are many enhancements and fixes in the latest version
    Hope that helps
    Richard
  8. Armysniper
    Armysniper avatar
    61 posts
    Member since:
    Mar 2005

    Posted 17 Feb 2011 Link to this post

    I would love to, but their implementation broke my application horribly and I dont have time to play with third party graphics when work needs to get done. :)

    That seems like a ton of code to do something way too simple and it should not matter what selection mode the user is using. Simple or Extended. It just defines how the Ctrl and Shift keys are used to select items.
  9. Answer
    Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 17 Feb 2011 Link to this post

    Hi,

    Well, the only thing I can say is that whilst the biggest updates were in Q2 2010, it has certainly been worth upgrading for us. There are so many enhancements and fixes, and from memory, I don't think Telerik support installations that are more than a year old by default.

    The code that actually selects in a multiextended is only
    List<int> selected = new List<int>(); 
    selected.Add(1); 
    selected.Add(3); 
    this.radListControl1.SelectedIndex = -1; // clear 
    foreach (RadListDataItem item in this.radListControl1.Items) 
        if (selected.Contains(Convert.ToInt32(item.Value))) 
        
            item.Selected = true
        
    }

    There are other ways to do this, but this I believe is similar to the Microsoft implementation. The Microsoft ListBox neither has SelectedValues that can be set.
    Richard
  10. Armysniper
    Armysniper avatar
    61 posts
    Member since:
    Mar 2005

    Posted 17 Feb 2011 Link to this post

    For us it broke so many UI elements that I had to undo the upgrade and revert so we could just work on code fixing (we are in maintenance mode).

    As for your method, I will try it since our return value is actually a list so it works for us. BUT with that said, there should be a way to select items by value in the control. Not having that seems less functional and not what I would expect from Telerik, who almost always have a slick property or method to get around a sticky situation.

    Thanks for your help!
  11. Armysniper
    Armysniper avatar
    61 posts
    Member since:
    Mar 2005

    Posted 17 Feb 2011 Link to this post

    Ahhh yes they changed the control completely it appears. You using the ListControl and i am using RadListBox which if I remember is deprecated in the future. Anyways, for posterity, here is the code I had to use based on yours.

    // Clear list box of any selections.
    lstStates.SelectedItems.Clear();
                  
    // Loop through all states and select ones from alert object.
    foreach (RadListBoxItem item in lstStates.Items)
    {
         // AlertInfo.States is a List<T>
         if (alertInfo.States.Contains(Convert.ToInt32(item.Value)))
         {
               lstStates.SelectedItems.Add(item);
         
    }
  12. Khalid
    Khalid avatar
    1 posts
    Member since:
    Oct 2016

    Posted 11 Oct in reply to Armysniper Link to this post

      public string RadDropDownSelectValue(RadDropDownList radDropDownList)
            {
                string str = "";
                foreach (RadListDataItem item in radDropDownList.SelectedItems)
                {
                    DataRowView dv = (DataRowView)item.Value;
                    str = dv.Row[0].ToString();
                }
                return str;
            }
  13. Hristo
    Admin
    Hristo avatar
    716 posts

    Posted 12 Oct Link to this post

    Hello Khalid,

    Thank you for sharing your code snippet. However, I am not sure how it is related to the discussed topic. Can you please explain if you need assistance? In case your question is not directly related to the one discussed here please open a new thread or a support ticket.

    Should you have further questions please do not hesitate to write back.

    Regards,
    Hristo Merdjanov
    Telerik by Progress
    Check out the Windows Forms project converter, which aids the conversion process from standard Windows Forms applications written in C# or VB to Telerik UI for WinForms.For more information check out this blog post and share your thoughts.
Back to Top
UI for WinForms is Visual Studio 2017 Ready