# Step 3 of 4: Bind the ListBox to the DataSet

## Content



To filter the categories from the data, complete the following steps:

1.  From the Toolbox, double-click the **ListBox** control to add it to the form. Dock it to the left of the **C1Chart** control so it appears like the following:
    
    ![](https://cdn.mescius.io/document-site-files/images/70bd33a2-280e-4cc3-a1c0-7b827eceb8aa/imagesext/bindinglistboxtodataset.png)
    
2.  Select the **ListBox** control and click on its smart tag to open the menu. Select **Use Data Bound items** and then in the **Data Source** drop-down listbox, select **Categories** from **Other Data Sources>Project Data Sources>categoriesDataSet**.
    
    ![](https://cdn.mescius.io/document-site-files/images/70bd33a2-280e-4cc3-a1c0-7b827eceb8aa/imagesext/categoriesdatasetinlistbox.png)
    
3.  Set the **DisplayMember** to **CategoryName**.
4.  Double-click on the **ListBox** to create a listbox1\_SelectedIndexChanged event.
5.  Add the following code in the **listbox1\_SelectedIndexChanged** event to filter the CategoryID to the listbox when the user selects a category item:
    
    DOC-DETAILS-TAG-OPEN
    
    DOC-SUMMARY-TAG-OPEN
    
    To write code in Visual Basic
    
    DOC-SUMMARY-TAG-CLOSE
    
    ```vbnet
    Private Sub listBox1_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles listBox1.SelectedIndexChanged
         If listBox1.SelectedIndex >= 0 Then
            Dim categoryID As String = Me.categoriesDataSet1.Categories(listBox1.SelectedIndex).CategoryID.ToString()
            Me.dataView1.RowFilter = "CategoryID = " + categoryID
            Me.c1Chart1.Header.Text = listBox1.Text
         End If
    End Sub
    ```
    
    DOC-DETAILS-TAG-CLOSE
    
    DOC-DETAILS-TAG-OPEN
    
    DOC-SUMMARY-TAG-OPEN
    
    To write code in C#
    
    DOC-SUMMARY-TAG-CLOSE
    
    ```csharp
    private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e)
    {
        if (listBox1.SelectedIndex >= 0)
        {
            string categoryID = this.categoriesDataSet1.Categories[listBox1.SelectedIndex].CategoryID.ToString();
            this.dataView1.RowFilter = "CategoryID = " + categoryID;
            this.c1Chart1.Header.Text = listBox1.Text;
        }
    }
    ```
    
    DOC-DETAILS-TAG-CLOSE
    
6.  In the **Form1\_Load** event add the following code to force the new calculation after the refill so the first category of product items, Beverages, appears rather than all of the unfiltered categories:
    
    DOC-DETAILS-TAG-OPEN
    
    DOC-SUMMARY-TAG-OPEN
    
    To write code in Visual Basic
    
    DOC-SUMMARY-TAG-CLOSE
    
    ```vbnet
    'force the new calculation after the refill
                                listBox1_SelectedIndexChanged(Me.listBox1, New EventArgs())
    ```
    
    DOC-DETAILS-TAG-CLOSE
    
    DOC-DETAILS-TAG-OPEN
    
    DOC-SUMMARY-TAG-OPEN
    
    To write code in C#
    
    DOC-SUMMARY-TAG-CLOSE
    
    ```csharp
    //force the new calcuation after the refill
                                listBox1_SelectedIndexChanged(this.listBox1, new EventArgs());
    ```
    
    DOC-DETAILS-TAG-CLOSE
    
7.  Run the application and select a category from the listbox to observe the chart filter the data.

Congratulations! You successfully bound data to the chart. In the next step you will modify the appearance of the chart.

![](https://cdn.mescius.io/document-site-files/images/70bd33a2-280e-4cc3-a1c0-7b827eceb8aa/imagesext/image9_3.png)

## See Also

[Step 4 of 4: Customize your Chart](/componentone/docs/win/online-chart2d/chartforwinformsquic/step4of4customizeyou)