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

Programmatically Selecting Items by Value in Listbox

12 Answers 1740 Views
ListControl
This is a migrated thread and some comments may be shown as answers.
Raventhorn
Top achievements
Rank 2
Raventhorn asked on 16 Feb 2011, 11:11 PM
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?

12 Answers, 1 is accepted

Sort by
0
Richard Slade
Top achievements
Rank 2
answered on 16 Feb 2011, 11:24 PM
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
0
Raventhorn
Top achievements
Rank 2
answered on 16 Feb 2011, 11:27 PM
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?
0
Richard Slade
Top achievements
Rank 2
answered on 17 Feb 2011, 12:09 AM
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
0
Raventhorn
Top achievements
Rank 2
answered on 17 Feb 2011, 06:27 PM
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.
0
Richard Slade
Top achievements
Rank 2
answered on 17 Feb 2011, 06:36 PM
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
0
Raventhorn
Top achievements
Rank 2
answered on 17 Feb 2011, 06:52 PM
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.
0
Accepted
Richard Slade
Top achievements
Rank 2
answered on 17 Feb 2011, 06:59 PM
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
0
Raventhorn
Top achievements
Rank 2
answered on 17 Feb 2011, 07:05 PM
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!
0
Raventhorn
Top achievements
Rank 2
answered on 17 Feb 2011, 07:12 PM
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);
     
}
0
Khalid
Top achievements
Rank 1
answered on 11 Oct 2016, 03:36 PM
  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;
        }
0
Hristo
Telerik team
answered on 12 Oct 2016, 08:09 AM
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.
0
Jay
Top achievements
Rank 1
Iron
Iron
answered on 14 Oct 2023, 05:13 AM | edited on 18 Oct 2023, 09:44 AM
None of this works.
Dess | Tech Support Engineer, Principal
Telerik team
commented on 16 Oct 2023, 04:37 AM | edited

Hi, Jay,

I am sorry to hear that you are facing any difficulties with the Telerik Ui for WinForms suite. However, it is not clear enough what is the exact requirement that you are trying to achieve. Could you please collaborate?

Once we get better understanding of the precise case, we would be able to think about a suitable approach and provide further assistance. Thank you.

Off topic, I would kindly ask you to keep any communication in the public forum in a polite manner in order to be as productive as possible.

 

Tags
ListControl
Asked by
Raventhorn
Top achievements
Rank 2
Answers by
Richard Slade
Top achievements
Rank 2
Raventhorn
Top achievements
Rank 2
Khalid
Top achievements
Rank 1
Hristo
Telerik team
Jay
Top achievements
Rank 1
Iron
Iron
Share this question
or