Click here to Skip to main content
Licence CPOL
First Posted 19 Apr 2008
Views 36,966
Downloads 804
Bookmarked 28 times

Fully data-bound CheckedListBox

By | 19 Apr 2008 | Article
BoundCheckedListBox is bindable to three tables representing a many-to-many data relation.
 
Part of The SQL Zone sponsored by
See Also

manytomanytest-src

Introduction

Maybe you already know that the .NET CheckedListBox doesn't provide any data binding, though it seems to be eligible in cases where you have a many-to-many data relation with a relation table; e.g., a product can be part of multiple categories, and each category can contain multiple products.

So, my task was to make the CheckedListBox bindable again.

Background

If you search for a bindable CheckedListBox on CodeProject, you will find two or three. I did this too. But when I took a closer look at them, I noticed that they don't implement a new data binding facility, but rather reactivate the listbox data binding to one single table - meaning that the list of items will be data bound. The checkboxes have to be set more or less manually. See ExCheckedListBox.aspx or http://www.codeproject.com/KB/grid/custCheckedListBox.aspx for details.

This is not what I wanted. I wanted the CheckedListBox to be fully data-bindable; e.g., holding a list of categories and automagically setting the checked states according to the data sources (one of them holding the currently selected product), and adding/removing entries of the relation table if the user (un)checks an item. Playing with the sample should make this clear.

So, the next question was how to create my own data binding. I had a basic idea on how this should work, but this article helped me a lot: http://www.codeproject.com/KB/database/DataBindCustomControls.aspx.

The BoundCheckedListBox does not bind to only one data source, it binds to three data sources and needs five data members.

Using the code

The sample shows which person loves which fruits.

So, we need:

  • A data source for the parent table, Personen
  • A data source for the child table, Fruechte
  • A data source for the relation table, p_f
  • A data member for the parent ID column, ID (Personen.ID)
  • A data member for the child ID column, ID (Fruechte.ID)
  • A data member for the child display column (holding the items shown in the ListBox), Name (Fruechte.Name)
  • Two data members for the two columns holding the IDs in the relation table

This is how you can set those (or via a property list in Design view):

boundCheckedListBox1.ChildDisplayMember = "Name";
boundCheckedListBox1.ChildValueMember = "fID";
boundCheckedListBox1.ParentValueMember = "pID";
boundCheckedListBox1.ParentIDMember = "ID";
boundCheckedListBox1.ChildIDMember = "ID";
boundCheckedListBox1.ParentDataSource = parentBindingSource;
boundCheckedListBox1.ChildDataSource = childBindingSource;
boundCheckedListBox1.RelationDataSource = relationBindingSource;

where each data source is a binding source, having a dataset as a data source and the corresponding table as the data member. Have a look at the source.

Points of interest

It is very important that the BoundCheckedListBox gets a currency manager from the data source and not a property manager (as that does not provide a list, an exception will be thrown). I figured out that using a DataSet.Table as the data source only wants to give me the property manager. So, I made use of binding sources.

Set the default values which don't throw exceptions if a new DataRow is created and no values are set. I can only call the currency manager's AddNew method, and the values are set after that.

Make use of the EndEdit functions of the binding sources. I have a button adding a new element to the child table and textboxes setting the name. E.g.:

EventHandler dataChangedHandler = new EventHandler(dataChanged);
....
button_kat_add.Click += dataChangedHandler;
button_kat_del.Click += dataChangedHandler;
....
textBox_kat_name.LostFocus += dataChangedHandler;
....

void dataChanged(object sender, EventArgs e) {
     dataTable1BindingSource.EndEdit();
     ....BindingSource.EndEdit();
     ....BindingSource.EndEdit();
     ....relationBindingSource.EndEdit();
}
......

private void button_kat_add_Click(object sender, EventArgs e) {
    this.....BindingSource.AddNew();
}

private void button_kat_del_Click(object sender, EventArgs e) {
     if (kategorienBindingSource.Current != null)
         this.kategorienBindingSource.RemoveCurrent();
}

If a checkbox is unchecked, the DataRow in the relation table is deleted. So we might end up with several deleted rows with the same IDs. This should be improved by un-deleting rows.

Feel free to make comments and updates!

History

  • 19.04.2008 - Initial version.

License

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

About the Author

vbar

Systems Engineer
Otto-von-Guericke University of Magdeburg
Germany Germany

Member

Student of Systems Engineering and Engineering Cybernetics

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
GeneralMinor bugfix PinmemberMember 444440417:59 5 Mar '09  
GeneralRe: Minor bugfix Pingroupstewart.page8:02 9 Aug '09  
GeneralRe: Minor bugfix Pinmembervbar10:15 10 Aug '09  
Generalstrange behavior on storing items into database Pinmemberai_mueller4:30 22 Feb '09  
GeneralRe: strange behavior on storing items into database Pinmembervbar6:31 22 Feb '09  
GeneralRe: strange behavior on storing items into database Pinmemberai_mueller9:38 22 Feb '09  
AnswerRe: strange behavior on storing items into database Pinmembervbar11:11 22 Feb '09  
QuestionWhat's wrong in my code PinmemberMonir Sabbagh0:37 12 Feb '09  
AnswerRe: What's wrong in my code PinmemberMonir Sabbagh8:54 12 Feb '09  
AnswerRe: What's wrong in my code Pinmembervbar11:22 12 Feb '09  
GeneralRe: What's wrong in my code Pinmembervbar11:31 12 Feb '09  
GeneralRe: What's wrong in my code PinmemberMonir Sabbagh2:14 14 Feb '09  
GeneralRe: What's wrong in my code Pinmembervbar8:01 15 Feb '09  
GeneralAwesome control PinmemberPhil Jeffrey20:31 10 Jul '08  
GeneralAdjust database PinmemberDe_paus_is_hip9:25 15 May '08  
GeneralRe: Adjust database Pinmembervbar10:58 15 May '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
Web01 | 2.5.120517.1 | Last Updated 19 Apr 2008
Article Copyright 2008 by vbar
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid