Windows > Windows Forms Forums > Windows Forms General > CheckedListbox in C# 2005
Ask a questionAsk a question
 

AnswerCheckedListbox in C# 2005

  • Wednesday, April 26, 2006 1:20 PMKriske Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    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

Answers

  • Tuesday, May 02, 2006 9:03 AMPeter Huang - MSFT Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    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

All Replies

  • Tuesday, May 02, 2006 9:03 AMPeter Huang - MSFT Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    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

  • Monday, May 21, 2007 4:08 PMYoungEngineer Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    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

  • Wednesday, August 08, 2007 8:11 AMDondor Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    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.

  • Tuesday, September 18, 2007 12:12 AMSianny Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    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.

     

     

  • Saturday, September 29, 2007 9:22 PMYoungEngineer Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    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.

  • Friday, December 14, 2007 9:20 PMmoz2 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    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();

    }

  • Tuesday, September 23, 2008 9:45 PMCeoj Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    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 30, 2008 11:14 PMBertly_the_Coder Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    LOL!! LOL!! You called him DUDE!!!!

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

    Cheers!!