📜  c# datagridview filter - C# (1)

📅  最后修改于: 2023-12-03 15:29:45.413000             🧑  作者: Mango

C# DataGridView Filter

DataGridView is a powerful control in C# that lets you create and display data in a table format. However, working with large amounts of data can be difficult without the ability to filter it. In this article, we will explore how to implement a filter in a C# DataGridView using different techniques.

Filtering Using the Default DataView

The easiest way to filter data in a DataGridView is by using the Default DataView. The DataView is a .NET Framework class used to sort, filter, and search DataTables. To use the Default DataView, follow these simple steps:

  1. Bind the DataGridView to a DataTable:

    DataTable dataTable = new DataTable();
    dataGridView.DataSource = dataTable;
    
  2. Create the Default DataView:

    DataView defaultView = dataTable.DefaultView;
    
  3. Set the filter criteria:

    defaultView.RowFilter = "column1 LIKE '%value%'";
    

    In this example, we filter the rows where 'column1' has the value 'value'. You can also use other operators like '=', '>', '<', etc.

  4. Bind the Default DataView to the DataGridView:

    dataGridView.DataSource = defaultView;
    

By default, the DataGridView will display the filtered rows.

Filter Using the BindingSource

Another way to filter data in a DataGridView is to use a BindingSource. A BindingSource is a .NET Framework class used to bind a control to a data source. To use the BindingSource, follow these steps:

  1. Bind the BindingSource to the DataGridView:

    BindingSource bindingSource = new BindingSource();
    dataGridView.DataSource = bindingSource;
    
  2. Bind the DataTable to the BindingSource:

    DataTable dataTable = new DataTable();
    bindingSource.DataSource = dataTable;
    
  3. Set the filter criteria:

    bindingSource.Filter = "column1 LIKE '%value%'";
    

    In this example, we filter the rows where 'column1' has the value 'value'. You can also use other operators like '=', '>', '<', etc.

By default, the BindingSource will display the filtered rows.

Filter Using a Custom Filter

If you require a more complex filter, you can use a custom filter. A custom filter allows you to define your own filter criteria. To use a custom filter, follow these steps:

  1. Create a method that filters the data:

    private void FilterData()
    {
        DataTable dataTable = new DataTable();
        // Add Data to DataTable
        DataView dataView = new DataView(dataTable);
        dataView.RowFilter = "column1 LIKE '%value%'";
        dataGridView.DataSource = dataView;
    }
    
  2. Call the method when filtering is required:

    FilterData();
    

In this example, we filter the rows where 'column1' has the value 'value'. You can modify the filter criteria to suit your needs.

Conclusion

In this article, we have explored different ways to filter data in a C# DataGridView. You can use the Default DataView, BindingSource, or create a custom filter depending on your requirements. A filter makes it easier to work with large amounts of data and improves the usability of your application.