Windows Forms: Advanced Filter DataGridView in C#

By FoxLearn 3/19/2024 3:48:37 PM   23.64K
This post shows you How to Filter DataGridView by columns in C# using Advanced DataGridView.

AdvancedDataGridView control is a .NET WinForms DataGridView with advanced Filtering and Sorting capabilities. It's a third-party libraries and extensions that provide advanced functionalities for DataGridView, which is a native control in Windows Forms for displaying and editing tabular data.

It offers additional features such as sorting, filtering, grouping, and hierarchical display capabilities, which are not available in the standard DataGridView.

How to use Advanced DataGridView to filter data in C#

Open your Visual Studio, then click New Project. Next, select Visual C# on the left, then Windows and then select Windows Forms Application. Name your project "AdvancedDataGridViewDemo" and then click OK

To play demo, you need to install AdvancedDataGridView by right-clicking on your project, then select Manage NuGet Packages -> Search AdvancedDataGridView -> Install

nuget advanceddatagridview

Advanced DataGridView is a custom of Windows Forms DataGridView Control with Excel-Like auto-filter context menu.

Here are the general steps to use the AdvancedDataGridView control in C#.

We will get data from sql database then populate it into Advanced DataGridView. Using the Advanced DataGridView control allows you to easily filter datagridview in c#

I'll create a dataset, then add the customers table from the Northwind database to my dataset.

c# dataset

DataSet is an in-memory cache of data retrieved from a data source, typically a database.

Advanced filter DataGridView in C#

After installing the AdvancedDataGridView, you need to rebuild your project.

You should now be able to find the AdvancedDataGridView control in your toolbox. Next, open your Form designer, then drag the AdvancedDataGridView from the Visual Studio toolbox onto your form designer.

filter datagridview in c#

Add a bindingsource control to the Advanced DataGridView allows you to fetch the customer data from the Northwind database.

northwind dataset

Finally, Add code to handle your winform as below

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

// datagridview with filtering capability c#
namespace AdvancedDataGridViewDemo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'northwindDataSet.Customers' table. You can move, or remove it, as needed.
            this.customersTableAdapter.Fill(this.northwindDataSet.Customers);

        }

        // DG advanceddatagridview sort
        private void advancedDataGridView_SortStringChanged(object sender, EventArgs e)
        {
            this.customersBindingSource.Sort = this.advancedDataGridView.SortString;
        }

        // RowFilter datagridview C#
        private void advancedDataGridView_FilterStringChanged(object sender, EventArgs e)
        {
            this.customersBindingSource.Filter = this.advancedDataGridView.FilterString;
        }

        private void customersBindingSource_ListChanged(object sender, ListChangedEventArgs e)
        {
            lblTotal.Text = string.Format("Total rows: {0}", this.customersBindingSource.List.Count);
        }
    }
}

Through this example you can easily use Advanced DataGridView to filter datagridview like excel in c#

VIDEO TUTORIALS