Question :
I have DataGridView
already with recovered data where the user selects an item. But because the items are numerous, I would like to allow the user to type over the DataGrid as the system “goes” to the item entered.
I do not 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 DataGridView
?
Thank you.
Answer :
Add a field of type TextBox
just above your DataGridView
and add a handler for the event TextChanged
and within this handler do as the code below, considering 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 changes lines that contain the value entered in the “Name” column, COL_NOME
is the name of a column, they are displayed. If the term is empty, that is, the person has deleted it, it will display all lines.