.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> Windows Form Controls >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Binding and check default values in CheckedListBox

Posted By:Akhil Raj       Posted Date: January 30, 2014    Points: 25    Category: Windows Form Controls    URL: http://www.dotnetspark.com  

Here i explained how to bind text / value combination checked list box and how to check one particular item based on value
 

To bind the collection you need to cas as listbox. The following is the code

                    ((ListBox)this.checkedListBox1).DataSource = myList;
                    ((ListBox)this.checkedListBox1).DisplayMember = "Name";
                    ((ListBox)this.checkedListBox1).ValueMember = "ID";
Here myList contains collections of MyItem which have Name to display and ID to set as value

If you need to check one particular item based on value you need to cast the item to your custom class then verify the value. The code is below

for (int i = 0; i < checkedListBox1.Items.Count; i++)
                        {
                            MyItem item = (MyItem)checkedListBox1.Items[i];
                            if (searchID == item.ID))
                            {
                                checkedListBox1.SetItemChecked(i, true);
                            }
                            else
                            {
                                checkedListBox1.SetItemChecked(i, false);
                            }

                        }
You can use this based on the situation. If you need to set only one value based on searchkey you can use break; key to escape from further loop.

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend