none
CheckedListbox in C# 2005

    Question

  • I ' m using checkedlistbox in VS 2005 as I used to do in VS 2003.

    But I no longer have checkedlistbox.DataSource to bind a datasource to this control. I would like to bind a datable to this control so that I can easily retrieve which datavalue has been been checked. Is there any way to obtain this again ?

    Thanks in advance

    Kriske

    Wednesday, April 26, 2006 1:20 PM

Answers

  • Hi,

    Here I assume you mean the Winform CheckedListBox.
    You may try to run the code below in a VS.NET 2005 winform application.
            private void button1_Click(object sender, EventArgs e)
            {
                DataTable dt = new DataTable();
                DataColumn dc = new DataColumn("StringType", typeof(String));
                dt.Columns.Add(dc);
                DataRow dr = dt.NewRow();
                dr[0] = "TestString";
                dt.Rows.Add(dr);
                this.checkedListBox1.DataSource = dt;
                this.checkedListBox1.DisplayMember = "StringType";
            }

    CheckedListBox.DataSource Property
    http://msdn2.microsoft.com/en-US/library/system.windows.forms.checkedlistbox.datasource(VS.80).aspx

    This property supports the .NET Framework infrastructure and is not intended to be used directly from your code.

    Gets or sets the data source for the control. This property is not relevant for this class.
    Version Information

    .NET Framework
    Supported in: 2.0, 1.1, 1.0

    If you still have any concern, please feel free to post here.

    Best regards,
    Peter Huang

    Tuesday, May 02, 2006 9:03 AM

All replies

  • Hi,

    Here I assume you mean the Winform CheckedListBox.
    You may try to run the code below in a VS.NET 2005 winform application.
            private void button1_Click(object sender, EventArgs e)
            {
                DataTable dt = new DataTable();
                DataColumn dc = new DataColumn("StringType", typeof(String));
                dt.Columns.Add(dc);
                DataRow dr = dt.NewRow();
                dr[0] = "TestString";
                dt.Rows.Add(dr);
                this.checkedListBox1.DataSource = dt;
                this.checkedListBox1.DisplayMember = "StringType";
            }

    CheckedListBox.DataSource Property
    http://msdn2.microsoft.com/en-US/library/system.windows.forms.checkedlistbox.datasource(VS.80).aspx

    This property supports the .NET Framework infrastructure and is not intended to be used directly from your code.

    Gets or sets the data source for the control. This property is not relevant for this class.
    Version Information

    .NET Framework
    Supported in: 2.0, 1.1, 1.0

    If you still have any concern, please feel free to post here.

    Best regards,
    Peter Huang

    Tuesday, May 02, 2006 9:03 AM
  • in Visual Studio 2005, the checkedlistbox control doesnt have a DataSource property.

    Basically, I am just trying to populate a checked list box with a sql query. So far I have been unsuccessful. Can anyone show me an example using a simple select sql query?

    thanks

    Monday, May 21, 2007 4:08 PM
  • DataSet ds = new DataSet();

    ds = Class1.getds("select * from Customers"); //Gets the dataset.

    for (int i = 0; i < ds.Tables[0].Columns.Count; i++)

    {

    checkedListBox1.Items.Add(ds.Tables[0].ColumnsIdea.ColumnName);

    }

     

     

    That I've done, what I'm having troubles with is getting the selected items back to a string so I can use in sql sentence.

    Wednesday, August 08, 2007 8:11 AM
  • HI,

    Did you have any luck with this answer. I am looking for the same thing myself. How do I bind the data and then make them correspond to an ID in the database and then automaticallym check based on the employee I am updating.

     

    Do I bind the data one by one as above by putting in position numbers and then give them a corresponding ID somehow?

     

    Thank you . I'm panicking.

     

     

    Tuesday, September 18, 2007 12:12 AM
  • Yes, the above solution worked for me. You should try it out and see if it conforms to what you want, and if it doesnt, if you post your exact problem, someone might be able to help you out.

    Saturday, September 29, 2007 9:22 PM
  • I'm sure you all have figured this out by now but the DataSource, DisplayMember, and ValueMember properties are all available for a CheckedListBox in C#.NET 2005.  It doesn't come up in intellisense but you can still use it.  I was writing all of this additional code and then I tried just setting those three properties to a datatable and two of it's columns.  Wala, it worked.  I posted some code below to help.

     

    DataTable myTable = new DataTable();

    myTable = className.loadDataTable();

     

    clstReportTypes.DataSource = myTable;

    clstReportTypes.DisplayMember = "ReportType";

    clstReportTypes.ValueMember = "ReportTypeID";

     

    You can retrieve the ID value of each checked item using the code below

     

    foreach (DataRowView myRow in clstReportTypes.CheckedItems)

    {

    do something with myRow[0].ToString();

    }

    Friday, December 14, 2007 9:20 PM
  • Dude! I've been searching for days trying to figure this out and here you have solved the problem in one fell swoop. Way to go and thanks!

     

    Tuesday, September 23, 2008 9:45 PM
  • LOL!! LOL!! You called him DUDE!!!!

    Good stuff man, I was not looking forward to figuring this out!!

    Cheers!!
    Tuesday, September 30, 2008 11:14 PM
  •  ((ListBox)chkLBoxCustomers).DisplayMember = "Name";
     ((ListBox)chkLBoxCustomers).ValueMember = "ID";
     ((ListBox)chkLBoxCustomers).DataSource = Customers.GetAll();
    
    This is the way to do.
    
    Tuesday, May 17, 2011 2:37 PM
  • You are binidng to a data table here but how I can bind it to a list?

    Any ideas plzz


    Saadie
    • Edited by Saadia Khan Wednesday, October 12, 2011 4:49 AM
    Wednesday, October 12, 2011 4:44 AM