Click here to Skip to main content
Licence CPOL
First Posted 16 Jan 2008
Views 37,175
Downloads 621
Bookmarked 29 times

A Custom CheckedListBox with Datasource Implementation (Bindable)

By Ricardo Cuello | 16 Jan 2008
In this article, you will see how to bind a CheckedListBox to the data source and how to get/set a list of IDs for the checked items.

1

2
1 vote, 12.5%
3
3 votes, 37.5%
4
4 votes, 50.0%
5
4.33/5 - 8 votes
μ 4.33, σa 1.30 [?]
CustomCheckedListBox

Introduction

Due to the fact that .NET CheckedListBox does not have out of the box binding facilities, this article implements an extension of the CheckedListBox which can be bindable. Moreover, it implements the DisplayMember and ValueMember properties in order to get an array of IDs containing the respective checked items.

Approach

We have to implement four extra properties:

  • DataSource
  • DisplayMember
  • ValueMember
  • ValueList

You already know the use of the first three but the last one is a List<int>. I chose int because we need the ID and most of the times in the lookup table, this ID is a number. I chose a List because the user can check more than one item, hence we need a list.

Using the Code

To use this code, you have to create an object whose type is cCheckedListBox and add it to your form:

cCheckedListBox cbGenreList = new cCheckedListBox();
cbGenreList.Location = new Point(8, 20);
cbGenreList.Size = new Size(130, 180);
this.grpGenres.Controls.Add(cbGenreList);

// Let's bind it to data from a Database
var GenreList = from c in databasebObjectContext.Genres orderby c.gnDescription select c;
cbGenreList.DataSource = GenreList.ToArray();
cbGenreList.DisplayMember = "gnDescription";
cbGenreList.ValueMember = "gnNumber";

To retrieve the values of the checked items :

List <int> selectedValues;
selectedValues= cbGenreList.ValueList;

To check some items by ID:

List<int> myValues = new List<int>(); 
myValues.Add(44);
myValues.Add(45);
myValues.Add(46);
cbGenreList.ValueList = myValues;

History

  • 17th January, 2008: Article posted

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Ricardo Cuello

Software Developer (Junior)

Canada Canada

Member


Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Questionwhat is the database schema look like. Pinmembermacupryk18:01 11 Jan '10  
GeneralSmall Mod PinmemberRoger Willcocks17:51 22 Apr '09  
GeneralGenerics Are Wonderful :) PinmemberRoger Willcocks17:12 22 Apr '09  
GeneralInitialization error PinmemberPenko Mitev7:16 18 Mar '09  
AnswerRe: Initialization error PinmemberRadu Martin5:00 31 Mar '09  
GeneralRe: Initialization error PinmemberPinx3:21 1 Oct '09  
QuestionBinding the list PinmemberMonir Sabbagh3:50 5 Feb '09  
AnswerRe: Binding the list PinmemberMonir Sabbagh6:31 7 Feb '09  
GeneralThanks PinmemberThomas Wells10:26 17 Jan '09  
Just what I needed. Though I can't really say I understand why exposing the DataSource and related properties just makes it work I'm glad it does.
GeneralList PinmemberJosé Filipe Néis4:11 5 Jun '08  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120210.1 | Last Updated 17 Jan 2008
Article Copyright 2008 by Ricardo Cuello
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid