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

Find Item By Content and Find Item By Value

3 Answers 716 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Rory
Top achievements
Rank 1
Rory asked on 12 Aug 2009, 12:42 AM
Hello,
I was wondering what is the best way to see whether a RadComboBoxItem exists inside a RadComboBox?

I can get the ComboBox.Items collection but I'm not seeing a method for determining whether an item that we are looking for is already in the list.
Thanks.

3 Answers, 1 is accepted

Sort by
0
Valeri Hristov
Telerik team
answered on 12 Aug 2009, 01:08 PM
Hi Rory,

You could use a simple Linq expression to check whether there is a certain RadComboBoxItem (or other object) present in the RadComboBox.Items collection:

using System.Linq;
...
bool exists = combo.Items.Any(item => (item as RadComboBoxItem).Content == "value");
if (exists)
{
...
}

Greetings,
Valeri Hristov
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
Chase
Top achievements
Rank 1
commented on 03 May 2023, 01:32 PM | edited

How are you doing a Linq query against an ItemCollection collection type?  I literally just pasted this line of code into my sandbox test app to see if this works (and it does not as you can see below, unless there is more than one RadComboBox?).

Here's the error as proof.   How are you making this work?

Martin Ivanov
Telerik team
commented on 05 May 2023, 06:47 AM

You can cast the Items collection to the corresponding item type. For example:

comboBox.Items.OfType<RadComboBoxItem>().Any(x => x.Content.Equals("value"));

Note that if the ItemsSource is populated with a collection of custom objects, the Items collection will contain these elements. In that case, you can use the corresponding data type in the OfType<T> method, and the data model property in the conditional statement.

comboBox.Items.OfType<MyItemInfo>().Any(x => x.MyContentProperty.Equals("value"));

 

0
Labcorp
Top achievements
Rank 1
answered on 06 Jan 2014, 04:59 PM
Using a similar example, How do you retrieve the index  of something that exists
0
Kalin
Telerik team
answered on 09 Jan 2014, 08:23 AM
Hello Labcorp,

In order to find the index of the desired Item you could use again a simple Linq expression. Please check the following code snippet:

var item = this.Combo.Items.FirstOrDefault(i => (i as RadComboBoxItem).Content.ToString() == "value");
if (item != null)
{   
    int indexOfItem = this.Combo.Items.IndexOf(item);   
}

Hope this helps.

Regards,
Kalin
Telerik
TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for SILVERLIGHT.
Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
Sign up for Free application insights >>
Tags
ComboBox
Asked by
Rory
Top achievements
Rank 1
Answers by
Valeri Hristov
Telerik team
Labcorp
Top achievements
Rank 1
Kalin
Telerik team
Share this question
or