CheckedDropDownList adding checked items to the list in the code behind

5 posts, 0 answers
  1. Jonathan
    Jonathan avatar
    2 posts
    Member since:
    May 2015

    Posted 02 May 2015 Link to this post

    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);
                        
                        
                        }

  2. Ralitsa
    Admin
    Ralitsa avatar
    176 posts

    Posted 04 May 2015 Link to this post

    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.

     
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Jonathan
    Jonathan avatar
    2 posts
    Member since:
    May 2015

    Posted 04 May 2015 in reply to Ralitsa Link to this post

    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.

  5. Yofeisi
    Yofeisi avatar
    1 posts
    Member since:
    Oct 2015

    Posted 19 Oct 2015 in reply to Ralitsa Link to this post

    Hi,  do not leave me "FirstOrDefault"
  6. Ralitsa
    Admin
    Ralitsa avatar
    176 posts

    Posted 21 Oct 2015 Link to this post

    Hello Yofeisi,

    Thank you for contacting us. 

    If your application targets .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
Back to Top
UI for WinForms is Visual Studio 2017 Ready