Click here to Skip to main content
12,999,157 members (44,419 online)
Click here to Skip to main content
Add your own
alternative version


33 bookmarked
Posted 23 May 2011

Multi Column Value Search in DataGridView

, 23 May 2011
Rate this:
Please Sign up or sign in to vote.
This post helps to search multiple cell values of the provided columns in a datagridview


This post will help you to search multiple cell values of the provided columns in a datagridview.


The Add/Delete button adds/deletes rows in the top datagridview, were the user can enter the search criteria.

The bottom datagridview is the grid in which we perform search.

The first column of the top datagridview lists all the column names of the bottom datagridview. The user has to enter the search value in the second column.

This property gets the search criteria from the top datagridview:

/// <summary>
/// Returns Dictionary(columnName, searchValue)
/// </summary>
private Dictionary<string, string> SearchList
        Dictionary<string, string> searchList = new Dictionary<string, string>();
        foreach (DataGridViewRow dgvRow in dataGridViewSearch.Rows)
            string colName = Convert.ToString(dgvRow.Cells["ColumnName"].Value);
            //ignore rows which have no "ColumnName" assigned
            if (colName != string.Empty)
                string val = Convert.ToString(dgvRow.Cells["Value"].Value);
                searchList.Add(colName, val);
        return searchList;

This routine performs the search operation and returns the matched datagridviewrows as list:

/// <summary>
/// Perform Search in the provided datagridview
/// with the Dictionary(columnName, searchValue)
/// </summary>
/// <param name="dgView"></param>
/// <param name="searchList"></param>
/// <returns>List of rows which satisfy the search criteria</returns>
private List<DataGridViewRow> PerformSearch
(DataGridView dgView, Dictionary<string, string> searchList)
    bool rowFound = false;
    List<DataGridViewRow> dgvSearchResult = new List<DataGridViewRow>();
    foreach (DataGridViewRow dgvRow in dgView.Rows)
        foreach (string colName in searchList.Keys)
            string val1 = Convert.ToString(dgvRow.Cells[colName].Value);
            string val2 = Convert.ToString(searchList[colName]);
            //Ignore case and compare values
            if (string.Compare(val1, val2, true) == 0)
                rowFound = true;
                //Stop searching other cell values, if in the wrong row
                rowFound = false;
        if (rowFound)
            dgvSearchResult.Add( dgvRow);
    return dgvSearchResult; 

Search Button Click event is defined below:

private void btnSearch_Click(object sender, EventArgs e)
    lblInfo.Text = string.Empty;

    //Check if the search column Name repeats
    List<string> colNameList = new List<string>();
    foreach (DataGridViewRow dgvRow in dataGridViewSearch.Rows)
        if (!colNameList.Contains(Convert.ToString(dgvRow.Cells["ColumnName"].Value)))
            MessageBox.Show("Invalid Search criteria", "Search", 
            MessageBoxButtons.OK, MessageBoxIcon.Information);

        //Perform search and get the result rows
        List<DataGridViewRow> resultRows = PerformSearch(dataGridView1, SearchList);
        //Deselect all Cells in the gridview
        if (this.dataGridView1.SelectedCells.Count > 0)
            foreach (DataGridViewCell dgvCell in this.dataGridView1.SelectedCells)
            dgvCell.Selected = false;

        if (resultRows.Count > 0)
            //Select the rows which satisfy the search criteria
            foreach (DataGridViewRow dgvRow in resultRows)
                dataGridView1.Rows[dgvRow.Index].Selected = true;

    lblInfo.Text = resultRows.Count.ToString() + " Match(s) Found";

The attached source code is in VS 2010.



This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Balu Sathish
Software Developer UVJ Technologies
India India
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralMy vote of 5 Pin
jayantbramhankar5-Jun-11 2:39
memberjayantbramhankar5-Jun-11 2:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170622.1 | Last Updated 23 May 2011
Article Copyright 2011 by Balu Sathish
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid