Click here to Skip to main content
Click here to Skip to main content

Tagged as

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

Introduction

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

image.png

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
{
    get
    {
        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;
            else
            {
                //Stop searching other cell values, if in the wrong row
                rowFound = false;
                break;
            }
        }
        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)))
            colNameList.Add(Convert.ToString(dgvRow.Cells["ColumnName"].Value));
        else
        {
            MessageBox.Show("Invalid Search criteria", "Search", 
            MessageBoxButtons.OK, MessageBoxIcon.Information);
            return;
        }

        //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.

Thanks,
Balu

License

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

Share

About the Author

Balu Sathish
Software Developer Calpine Technologies
India India
No Biography provided

Comments and Discussions

 
GeneralMy vote of 5 Pinmemberjayantbramhankar5-Jun-11 2:39 

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

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

| Advertise | Privacy | Mobile
Web03 | 2.8.140814.1 | Last Updated 23 May 2011
Article Copyright 2011 by Balu Sathish
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid