1
I have a datagridview
which is populated from a list with data. When starting the screen I make a SELECT
searching all bank records. How do I perform a search on this list and datagridview
?
My datagridView is filled so:
gridBackups2.DataSource = listaAux;
for (int i = 0; i < listaAux.Count; i++)
{
for (int j = 0; j <= gridBackups2.ColumnCount; j++)
{
if (j == 0)
{
gridBackups2.Rows[i].Cells[j].Value = listaBackupAtivos[i].id + "";
}
if (j == 1)
{
gridBackups2.Rows[i].Cells[j].Value = listaBackupAtivos[i].customer + "";
}
if (j == 2)
{
gridBackups2.Rows[i].Cells[j].Value = getCliente(listaBackupAtivos[i].customer);
}
if (j == 3)
{
gridBackups2.Rows[i].Cells[j].Value = listaBackupAtivos[i].scheduleTime;
}
if (j == 4)
{
gridBackups2.Rows[i].Cells[j].Value = retornaStatus(listaBackupAtivos[i].status);
}
}
}
To grid
is being filled in correctly (ID, name, Date). How do I conduct a search, for example by the name that is filled in the grid, without I need to perform another select? Because if every time I go to research I need to make a select, it will be very slow. Is there any way I can search directly on the list I upload when I start the program?
EDIT:
private void txPesquisar_TextChanged(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt = (DataTable) gridBackups2.DataSource;
System.Console.WriteLine("Teste dt> " + dt.Columns.Count);
System.Console.WriteLine("Teste grid> " + gridBackups2.Columns.Count);
dt.DefaultView.RowFilter = $"Cliente like '%{txPesquisar.Text}%'";
}
Good morning John, I made the changes but did not succeed. When I will do the error search: There was an untreated "System.Data.Evaluateexception" exception in System.Data.dll The column [Client cannot be found]. I performed a test: System.Console.Writeline("Test dt> " + dt.Columns.Count); System.Console.Writeline("Test grid> " + gridBackups2.Columns.Count); And the result was: Test dt> 0 Test grid> 5 Client. Can you tell me what’s wrong?
– Andre Luiz
This is the event in the Textbox field private void txPesquisar_TextChanged(Object Sender, Eventargs and) { Datatable dt = new Datatable(); dt = (Datatable) gridBackups2.Datasource; System.Console.Writeline("Test dt> " + dt.Columns.Count); System.Console.Writeline("Test> " + gridBackups2.Columns.Count); dt.DefaultView.Rowfilter = $"Client like '%{txPsearch.Text}%'"; }
– Andre Luiz
And the column
Cliente
exists in yourDataTable
?– João Martins
The column name exists in gridBackups2, but Datatable comes with zero columns
– Andre Luiz
And you’re assigning the
DataSource
correctly toDataGridView
?– João Martins