locked
Multiselect in checkBoxList RRS feed

  • Question

  • Hello

    I'm trying to bind the table to checkBoxList as a value and a member (for example: binding a table with columns JobID and JobNumber ) and return both the values in checkbox. Is there any way for multiselect in checkBoxList.   

    Thursday, January 28, 2016 5:10 PM

Answers

  • Hi harsha kondeti,

    Base on your description, you want to know how to bind datasource to checkedlistbox and get the checked items' valuemember and displaymember.

    >>I'm trying to bind the table to checkBoxList

    You could refer to the following code:

                DataTable dt = new DataTable();
    
                dt.Columns.Add("key");
    
                dt.Columns.Add("text");
    
                dt.Rows.Add(1, "test");
    
                dt.Rows.Add(2, "abc");
    
                this.checkedListBox1.DataSource = dt;
    
                this.checkedListBox1.DisplayMember = "text";
    
                this.checkedListBox1.ValueMember = "key";
    

    >> and return both the values in checkbox

    GetValue method:

            private void btnGetValue_Click(object sender, EventArgs e)
    
            {
    
                richTextBox1.Clear();
    
                for (int i = 0; i < this.checkedListBox1.Items.Count; i++)
    
                {
    
                    if (this.checkedListBox1.GetItemChecked(i))
    
                    {
    
                        this.checkedListBox1.SetSelected(i, true);
    
                        richTextBox1.AppendText("Value: " + this.checkedListBox1.SelectedValue.ToString() + ",");
    
                    }
    
                }
    
            }
    

    GetText method:

            private void btnText_Click(object sender, EventArgs e)
    
            {
    
                richTextBox1.Clear();
    
                for (int i = 0; i < checkedListBox1.Items.Count; i++)
    
                {
    
                    if (this.checkedListBox1.GetItemChecked(i))
    
                    {
    
                        richTextBox1.AppendText("Key: " + this.checkedListBox1.GetItemText(this.checkedListBox1.Items[i]) + ",");
    
                    }
    
                }
    
            }
    

    Get Item:

            for (int i = 0; i < checkedListBox1.Items.Count; i++)
    
                {
    
                    if (this.checkedListBox1.GetItemChecked(i))
    
                    {
    
                        this.checkedListBox1.SetSelected(i, true);
    
                        richTextBox1.AppendText("Key: " + this.checkedListBox1.GetItemText(this.checkedListBox1.Items[i]) + ",");
    
                        
    
                        richTextBox1.AppendText("Value: " + this.checkedListBox1.SelectedValue.ToString() + ";");
    
                    }
    
                }
    

    Screenshot:

    If you want know more about checkedlistbox,you could read the following link:

    https://msdn.microsoft.com/en-us/library/system.windows.forms.checkedlistbox(v=vs.110).aspx

    Regards,

    Moonlight


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Friday, January 29, 2016 2:49 AM