Question Update DataGridView with Selected CheckedListBox

Kitty2016

New member
Joined
Sep 4, 2016
Messages
4
Programming Experience
1-3
Hi,

I have a datagridview table on my windows forms and a checklistbox.

When a user clicks items from the checklist box I want it to display only them items in the datagridview.

I currently have it so that it goes through the checkboxlist to check which ones are ticked (it also creates a textbox and puts the value of the checked item in an individual textbox)

But it only displays one of the items I check.

Here is my current code:

C#:
        private void lstDropDown_SelectedIndexChanged(object sender, EventArgs e)
        {


            for (int i = 0; i < lstDropDown.CheckedItems.Count; i++)


       
            {


                //Create textbox
                TextBox textBox = new TextBox();
                //Position textbox on screen
                textBox.Left = 120;
                textBox.Top = (i + 1) * 20;
                //Add controls to form
                this.Controls.Add(textBox);



                textBox.Text = (lstDropDown.CheckedItems[i].ToString());


                dtRecord.DefaultView.RowFilter = string.Format("Section = '{0}'" , textBox.Text);
            }

Any help is appreciated.

Thank you.
 
Firstly, your title mentioned a GridView and CheckBoxList, which are both Web Forms server controls. The WinForms controls are DataGridView and CheckedListBox (not CheckListBox). Using the wrong names for things is a great way to cause confusion.

Secondly, the SelectedIndexChanged event is a poor choice in this instance. Selected and checked are two different things so don't use an event related to selection when you're interested in what's checked. The ItemCheck event is the correct one to handle in this case, but you should read about it because it might not work how you expect.

Thirdly, there's no use looping through the CheckedItems and setting the RowFilter for each one because you're just going to end up with the last one and only the last one being included as you overwrite the filter each time.

The correct approach would look something like this:
Private filterValues As New List(Of String)

Private Sub CheckedListBox1_ItemCheck(sender As Object, e As ItemCheckEventArgs) Handles CheckedListBox1.ItemCheck
    Dim filterValue = Me.CheckedListBox1.GetItemText(Me.CheckedListBox1.Items(e.Index))

    If e.NewValue = CheckState.Checked Then
        Me.filterValues.Add(filterValue)
    Else
        Me.filterValues.Remove(filterValue)
    End If

    Dim filterExpression As String

    If Me.filterValues.Count > 0 Then
        filterExpression = String.Format("Section IN ('{0}')",
                                         String.Join("', '",
                                                     Me.filterValues))
    End If

    'Use filterExpression here.
End Sub
I would tend to suggest using 'filterExpression' to set the Filter of a BindingSource but you can set the RowFilter of a DataTable if you prefer.
 
Thank you and I apologise for the incorrect control names :)

I am currently getting this error:

Error 1 Non-invocable member 'System.Windows.Forms.CheckedListBox.Items' cannot be used like a method
 
Oops! I forgot I was on our C# site and I provided VB code. Here's the C# equivalent:
private List<string> filterValues = new List<string>();

private void CheckedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
{
    var filterValue = this.CheckedListBox1.GetItemText(this.CheckedListBox1.Items[e.Index]);

    if (e.NewValue == CheckState.Checked)
        this.filterValues.Add(filterValue);
    else
        this.filterValues.Remove(filterValue);

    string filterExpression = null;

    if (this.filterValues.Count > 0)
        filterExpression = string.Format("Section IN ('{0}')", string.Join("', '", this.filterValues));

    // Use filterExpression here.
}
 
Back
Top Bottom