none
Databinding CheckedListBox .Net 2005

    Question

  • I need to populate a checkedListBox with values from the database.

    The data is like this

    ID               Name
    1                Mona
    2                Lilly
    3                Veronica

    I want the name to be stored in the DisplayMember property and the ID in the ValueMember property. Since the Datasource property is no more supported by the checkedListBox  in .Net2005 I don't know how to accomplish this.

    If I use the 2nd overloaded method of the checkedListBox.DataBindings.Add(), what do I write in the String PropertyName parameter for a checkbox?

     

     

     

    Wednesday, June 14, 2006 5:53 AM

Answers

  • I have to disagree with Blair... in this case it seems like Neelanjana only wants to fill the list automatically with a DataTable or DataSet, and I'm guessing that the checked status is going to be handled by code using the appropiate event instead of directly binding to the datasource.

    Whatever the reason for doing the databinding, the datasource property is still there, only that the IntelliSense does not show it... (don't know why :S)

    You can use this code Neelanjana:

    checkedListBox1.DataSource = dtDataTable; // Your datatable or dataset

    checkedListBox1.DisplayMember = "Name";

    checkedListBox1.ValueMember = "ID";

    Hope that helps,

    Tuesday, June 20, 2006 7:28 PM

All replies

  • for just a moment, take the reverse into consideration. If there were a datasource, what field in your dataset would you bind the checkbox to???

    CheckedListBox has never had a datasource property. And for good reason - it makes no sense.

    You can probably get the desired functionality via a standard ListBox with a non default selection mode. yes it wont have a check box, but it will look and act like typical multiselect data aware controls. 

     

    Wednesday, June 14, 2006 6:22 AM
  • I have to disagree with Blair... in this case it seems like Neelanjana only wants to fill the list automatically with a DataTable or DataSet, and I'm guessing that the checked status is going to be handled by code using the appropiate event instead of directly binding to the datasource.

    Whatever the reason for doing the databinding, the datasource property is still there, only that the IntelliSense does not show it... (don't know why :S)

    You can use this code Neelanjana:

    checkedListBox1.DataSource = dtDataTable; // Your datatable or dataset

    checkedListBox1.DisplayMember = "Name";

    checkedListBox1.ValueMember = "ID";

    Hope that helps,

    Tuesday, June 20, 2006 7:28 PM
  •  Daniel Rodriguez wrote:

    I have to disagree with Blair... in this case it seems like Neelanjana only wants to fill the list automatically with a DataTable or DataSet, and I'm guessing that the checked status is going to be handled by code using the appropiate event instead of directly binding to the datasource.

    Whatever the reason for doing the databinding, the datasource property is still there, only that the IntelliSense does not show it... (don't know why :S)

    You can use this code Neelanjana:

    checkedListBox1.DataSource = dtDataTable; // Your datatable or dataset

    checkedListBox1.DisplayMember = "Name";

    checkedListBox1.ValueMember = "ID";

    Hope that helps,

    WOW Thank you, i was suffering about the same

    Thursday, July 06, 2006 6:31 PM
  • Daniel, You trick works very good but how do you get the checked box value? 

    I did this  checkedListBox1.SelevetedValue.ToString()  and it is not working? 

     

    Tuesday, September 25, 2007 3:13 PM
  • Iterate through the CheckedItems collection.  Each Object in the bound control is actually a boxed DataRowView object.  For example:

    For Each objItem As Object In checkedListBox1.CheckedItems

    Dim drv As DataRowView = DirectCast(objItem, DataRowView)

    Dim intID As Integer = CInt(drv("ID"))

    Next


    Sunday, October 21, 2007 7:35 PM
  • Hi Everyone,

     

    This is great... It is very useful one for me... I was looking for the same..

    Thanks to all...

     

    Priya

    Thursday, June 12, 2008 6:33 PM
  • Many Thanks.

    It all really helped me out.

     

    Regards,

    Rajat

    Tuesday, July 08, 2008 7:42 PM
  • Au contraire - the checklistbox is derived from the listbox, which has a datasource.  They are much more user-friendly than the 80's style inverse video of a listbox.  What's the reasoning behind the "do not use directly" property ?  Functionally they are practically identical so how does listbox support the checked property when binding to a datasource ?
     
    I would think this would be propagated to the checklistbox.
    • Proposed as answer by MOPO3OB Friday, May 15, 2009 2:18 PM
    Saturday, September 20, 2008 5:32 PM
  • // DataBinding

    (CheckedListBox as ListBox).DataSource = ds.Tables["myTable"];
    (CheckedListBox as ListBox).DisplayMember = "Name";
    (CheckedListBox as ListBox).ValueMember = "Id";

    // DataAcces

    foreach (DataRowView item in CheckedListBox.CheckedItems)
    {
        result += ("'" + item.Row["id"] + "',");
    }



    • Edited by MOPO3OB Friday, May 15, 2009 2:29 PM
    Friday, May 15, 2009 2:27 PM
  • // DataBinding

    (CheckedListBox as ListBox ) . DataSource = ds . Tables [ "myTable" ];
    ( CheckedListBox as ListBox ) . DisplayMember = "Name" ;
    ( CheckedListBox as ListBox ) . ValueMember = "Id" ;

    // DataAcces

    foreach ( DataRowView item in CheckedListBox . CheckedItems )
    {
        result += ( "'" + item . Row [ "id" ] + "'," );
    }



    Thanks!!! me sirvio de mucho ahora lo voy a pasar a Basic xD
    Wednesday, July 08, 2009 10:17 PM