Windows Forms: Search DataGridView by using ComboBox and TextBox in C#

By FoxLearn 7/28/2017 9:34:53 PM   14.62K
How to Search/Filter DataGridView by using ComboBox and TextBox in C#.

Step 1Click New Project, then select Visual C# on the left, then Windows and then select Windows Forms Application. Name your project "SearchDataGridViewUsingComboBoxTextBox" and then click OK

search datagridview in c#Step 2: Design your form as below

c# search datagridview

Step 3: Create a new dataset, then add a Product table from Northwind database to your dataset

c# dataset

Step 4: Add code to handle your form 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;

namespace SearchDataGridViewUsingComboBoxTextBox
{
    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 'database.Products' table. You can move, or remove it, as needed.
            this.productsTableAdapter.Fill(this.database.Products);
            cboColumn.SelectedIndex = 1;
        }

        private void txtSearch_KeyDown(object sender, KeyEventArgs e)
        {
            //Filter data
            if (e.KeyCode == Keys.Enter)
            {
                if (string.IsNullOrEmpty(txtSearch.Text))
                    productsBindingSource.Filter = string.Empty;
                else
                    productsBindingSource.Filter = string.Format("{0}='{1}'", cboColumn.Text, txtSearch.Text);
            }
        }
    }
}

VIDEO TUTORIALS