Click here to Skip to main content
15,867,488 members
Articles / Programming Languages / C#
Article

A Custom CheckedListBox with Datasource Implementation (Bindable)

Rate me:
Please Sign up or sign in to vote.
4.50/5 (9 votes)
16 Jan 2008CPOL 97.4K   2.7K   31   11
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.
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:

C#
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 :

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

To check some items by ID:

C#
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)


Written By
Software Developer (Junior)
Canada Canada
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questionobject reference not set to an instance of an object Pin
Member 1313596223-Apr-17 23:34
Member 1313596223-Apr-17 23:34 
Questionwhat is the database schema look like. Pin
macupryk11-Jan-10 17:01
macupryk11-Jan-10 17:01 
GeneralSmall Mod Pin
Roger Willcocks22-Apr-09 16:51
Roger Willcocks22-Apr-09 16:51 
As described in this article, if you assign the DisplayMember BEFORE you assign the DataSource, the DisplayMember is wiped (ValueMember is not, go figure). Make this change to preserve it.

EDIT: point to note, this is a behaviour of the base ListControl, not of CheckedListBox

[DefaultValue("")]
[AttributeProvider(typeof(IListSource))]
[RefreshProperties(RefreshProperties.All)]
[Browsable(true)]
public new object DataSource
{
    get
    {
        return base.DataSource;
    }
    set
    {
        string dm = this.DisplayMember;
        ((ListBox)this).DataSource = value;
        if (dm != this.DisplayMember)
        {
            this.DisplayMember = dm;
        }
    }
}


Roger Willcocks
Software Engineer
MCP, MCSD .NET
http://www.l-space-design.com/

GeneralGenerics Are Wonderful :) Pin
Roger Willcocks22-Apr-09 16:12
Roger Willcocks22-Apr-09 16:12 
GeneralInitialization error Pin
Penko Mitev18-Mar-09 6:16
Penko Mitev18-Mar-09 6:16 
AnswerRe: Initialization error Pin
Martin Radu31-Mar-09 4:00
Martin Radu31-Mar-09 4:00 
GeneralRe: Initialization error Pin
pinx1-Oct-09 2:21
pinx1-Oct-09 2:21 
QuestionBinding the list Pin
Monir Sabbagh5-Feb-09 2:50
Monir Sabbagh5-Feb-09 2:50 
AnswerRe: Binding the list Pin
Monir Sabbagh7-Feb-09 5:31
Monir Sabbagh7-Feb-09 5:31 
GeneralThanks Pin
Thomas Wells17-Jan-09 9:26
Thomas Wells17-Jan-09 9:26 
GeneralList<int></int> Pin
José Filipe Néis5-Jun-08 3:11
José Filipe Néis5-Jun-08 3:11 

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

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