5,660,782 members and growing! (19,267 online)
Email Password   helpLost your password?
Desktop Development » Grid & Data Controls » DataSets, DataGrids etc     Intermediate License: The Code Project Open License (CPOL)

A Custom CheckedListBox with Datasource Implementation (Bindable)

By Ricardo Cuello

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.
C# (C# 3.0, C#), .NET (.NET, .NET 3.5), Dev

Posted: 16 Jan 2008
Updated: 16 Jan 2008
Views: 8,985
Bookmarked: 11 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
3 votes for this Article.
Popularity: 2.07 Rating: 4.33 out of 5
0 votes, 0.0%
1
0 votes, 0.0%
2
0 votes, 0.0%
3
1 vote, 33.3%
4
2 votes, 66.7%
5
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



Occupation: Software Developer (Junior)
Location: Canada Canada

Other popular Grid & Data Controls articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 1 of 1 (Total in Forum: 1) (Refresh)FirstPrevNext
GeneralListmemberJosé Filipe Néis4:11 5 Jun '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 16 Jan 2008
Editor: Deeksha Shenoy
Copyright 2008 by Ricardo Cuello
Everything else Copyright © CodeProject, 1999-2008
Web15 | Advertise on the Code Project