Search in Datagridview populated by typing on it

Asked

Viewed 1,098 times

4

I own a DataGridView already with recovered data where user selects an item. But, as the items are numerous, I would like to enable the user to type on the Datagrid while the system "goes" to the entered item.

I don’t know the correct name for this type of facility, but it’s very common in the Widows environment itself. How do I apply this to a DataGridView?

Thank you.

  • Like a CTRL F on the page?

  • That, but without the CTRL + F.. Hehe

  • Where would the person type? in gridview? itself or in a search field outside?

  • In the Grid itself..

1 answer

1

Add a field of the type TextBox just above your DataGridView and add a handler to the event TextChanged and within this handler do as code below, taking into account your needs:

var termo = (sender as TextBox).Text.ToLowerInvariant();
bool semTermo = String.IsNullOrEmpty(termo);

foreach (DataGridViewRow linha in dataGridView1.Rows)
{
    if ((linha.Cells[COL_NOME.Index].Value as string).ToLowerInvariant().Contains(termo) || semTermo)
        linha.Visible = true;
    else
        linha.Visible = false;
}

In this example when the field text is changed the lines that contain the value typed in the "Name column", COL_NOME is the name of a column, will be displayed. If the term is empty, ie the person has deleted, it will display all lines.

  • The idea is interesting, but when testing the code I have the error "It was not possible to render invisible the line associated with the position of the currency manager". I tried to add the line dataGridView1.CurrentCell = null just above Visible = False (solution I found there), but it did not work

  • "... currency manager" you are using some extra component or are not using the original DGV?

  • Strange the mistake, no? Also not understood. Nothing different, only the old and almost obsolete Datagridview..

  • Post Exception in English, without translation.

  • I added a little bit that I found HERE and solved the error, but the whole process takes too long in analyzing rows. There would be no way to just position the Grid on the data sought?

  • Yes, I would, but I believe the delay is due to the search itself and not the concealment of the lines. You want to position the cell that is first found as the visible cell is this?

  • My initial idea was to leave none row invisible, just position the searched item on the screen, however, I ended up doing a search via TextBox that brings about a new query in the database at the event TextChanged.

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.