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

CheckedDropDownList adding checked items to the list in the code behind

4 Answers 450 Views
CheckedDropDownList
This is a migrated thread and some comments may be shown as answers.
Jonathan
Top achievements
Rank 1
Jonathan asked on 02 May 2015, 08:40 AM

Hi

 

I have been using a CheckedDropDownList in an application and I want to check some of the values in the dropdownlist before the users sees it. I have tried several things in order to get it work and so far nothing. It should b a simple thing i just don't know..

 

so far i have tried

 

 List<string> dbValues = _asset.MultiLocDrawingNum;

                    foreach (var v in dbValues)
                    {
                        var item = ddData.Items.FirstOrDefault(x => x.Text == v);

                        item.Selected = true;

                        ddData.Items.Add(item);

                        //ddData.SelectedItem = item;

                        //ddData.SelectedIndex = ddData.FindString(item.Text);
                    
                    
                    }

4 Answers, 1 is accepted

Sort by
0
Ralitsa
Telerik team
answered on 04 May 2015, 08:20 AM
Hi Jonathan,

Thank you for contacting us. 

The Checked property indicates whether the item is checked. You need to set the property to true to show the items. Here is the code snippet how you can achieve it: 
private void radButton1_Click(object sender, EventArgs e)
{
    List<string> dbValues = new List<string>();
    dbValues.Add("Cafe mocha");
    dbValues.Add("Americano");
 
    foreach (var v in dbValues)
    {
        RadCheckedListDataItem item = this.radCheckedDropDownList1.Items.FirstOrDefault(x => x.Text == v) as RadCheckedListDataItem;
        item.Checked = true;              
    }
}

Should you have further questions, I would be glad to help.

Regards,
Ralitsa
Telerik
 

See What's Next in App Development. Register for TelerikNEXT.

 
0
Jonathan
Top achievements
Rank 1
answered on 04 May 2015, 09:23 AM

Thank you, this was exactly what i needed, i tried getting access to the checked attribute with no luck. However i was using the wrong class and i wasn't aware that there was another one so this is great!

 

Many thanks.

0
Yofeisi
Top achievements
Rank 1
answered on 19 Oct 2015, 03:24 PM
Hi,  do not leave me "FirstOrDefault"
0
Ralitsa
Telerik team
answered on 21 Oct 2015, 07:51 AM
Hello Yofeisi,

Thank you for contacting us. 

If your application Net 2.0, you can not use FirstOrDefault. Please refer to the code snippet below how you can change the code: 
private void radButton1_Click(object sender, EventArgs e)
{
    List<string> dbValues = new List<string>();
    dbValues.Add("Cafe mocha");
    dbValues.Add("Americano");
 
    //foreach (var v in dbValues)
    //{
    //    RadCheckedListDataItem item = this.radCheckedDropDownList1.Items.FirstOrDefault(x => x.Text == v) as RadCheckedListDataItem;
    //    item.Checked = true;
    //}
 
    foreach (var text in dbValues)
    {
        foreach (RadCheckedListDataItem item in this.radCheckedDropDownList1.Items)
        {
            if (item.Text == text)
            {
                item.Checked = true;
            }
        }
    }
}

Let me know if I can assist you further.

Regards,
Ralitsa
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
CheckedDropDownList
Asked by
Jonathan
Top achievements
Rank 1
Answers by
Ralitsa
Telerik team
Jonathan
Top achievements
Rank 1
Yofeisi
Top achievements
Rank 1
Share this question
or