How to search in DataGridView using WindowsForms with the data already loaded?

Viewed 75

I know you will be thinking that this question is already duplicated but Nope. I want to implement a search method to find data in my DataGridView using WindowsForms but most of the solutions I found over the internet they use code to search in the database. There is not point to search in the database when data is already loaded in the DataGridView.

I found this: How to search in Data gridview in C# Windows Form application?

but code from link didn't work for me (No errors, just does nothing):

    private void textBox1_TextChanged(object sender, EventArgs e)
    {
        BindingSource bs = new BindingSource();
        bs.DataSource = Listaventas.DataSource;
        bs.Filter = "Nombre" + " like '%" + textBox1.Text + "%'";
        Listaventas.DataSource = bs;

    }

I am using Entity Framework to load data in my DataGridView, but I want to make a search in the DataGridView with the data already loaded.

This is my code:

    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);

        BindingSource source = new BindingSource();
        var ventas = _facturasRepository.GetListVentas();
        source.DataSource = ventas;
        Listaventas.DataSource = typeof(List<>); 
        Listaventas.DataSource = source;
        Listaventas.Columns.Add("Column", "Test");
        Listaventas.Columns["Column"].DefaultCellStyle.NullValue = "Add";
    }

    public IList<Factura> GetListVentas()
    {
       return _context.Facturas.OrderByDescending(a => a.FechaVenta).ToList();
    }

enter image description here

I am trying to make a search in the DataGridView with the data already loaded. I think the search should accept all columns text. Most internet solutions tries to make a search by making a database call, but I do not find any sense in my case because I have data already loaded in the component.

How do I code this?

0 Answers
Related