C# - Windows Form how to search in a Datagridview

Asked

Viewed 650 times

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}%'"; 
}

1 answer

2

The simplest way to filter a DataGridView is using a DataTable, which allows root filtering, sorting columns, etc.

How, apparently, you’re using a List, then it is necessary to implement a conversion method for DataTable:

private DataTable ToDataTable<T>(IList<T> data)
{
    PropertyDescriptorCollection props = TypeDescriptor.GetProperties(typeof(T));
    DataTable table = new DataTable();

    for (int i = 0; i < props.Count; i++)
    {
        PropertyDescriptor prop = props[i];
        table.Columns.Add(prop.Name, prop.PropertyType);
    }

    object[] values = new object[props.Count];

    foreach (T item in data)
    {
        for (int i = 0; i < values.Length; i++)
            values[i] = props[i].GetValue(item);

        table.Rows.Add(values);
    }

    return table;
}

To assign the Datasource:

DataTable dt = ToDataTable(listAux);
gridBackups2.DataSource = dt;

To filter the results:

(gridBackups2.DataSource as DataTable).DefaultView.RowFilter = $"coluna like '%{txtFiltro.Text}%'";

Where the coluna represents the name of the column you want to filter and the control txtFiltro to TextBox where you are setting the filter.

With a DataTable defined as DataSource can filter results in real time without additional code.

  • 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?

  • 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}%'"; }

  • And the column Cliente exists in your DataTable?

  • The column name exists in gridBackups2, but Datatable comes with zero columns

  • And you’re assigning the DataSource correctly to DataGridView?

Browser other questions tagged

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