Datagrid textbox search C#

I'm working on an Application for school and i have a datagrid connected to a local SQL database.

With the click of a button my data appears in the data grid, This all goes well.

The problem is that I want to be able to search specific customer ID's.

I'm fairly new to programming. I have googled for some hours but i can't find a good way to use most codes.

Does anyone know a simple way to add a filter on a textbox wich will then corrensponds with my DataGridView?

Upvotes: 0

Views: 1038

Answers (1)

Jacques Bronkhorst
Jacques Bronkhorst

Reputation: 1695

This will give you the gridview row index for the value:

String searchValue = "somestring";
int rowIndex = -1;
foreach(DataGridViewRow row in DataGridView1.Rows)
{
    if(row.Cells[1].Value.ToString().Equals(searchValue))
    {
        rowIndex = row.Index;
        break;
    }
}

Or a LINQ query

    int rowIndex = -1;

    DataGridViewRow row = dgv.Rows
        .Cast<DataGridViewRow>()
        .Where(r => r.Cells["SystemId"].Value.ToString().Equals(searchValue))
        .First();

    rowIndex = row.Index;

then you can do:

 dataGridView1.Rows[rowIndex].Selected = true;

Upvotes: 2

Related Questions