How to search data in any column of datagridview, after popular it by the c#database

Asked

Viewed 535 times

0

I have a datagridview that takes the data from the sqlserver database and needed to make queries, but as they are a lot of data we can’t keep pulling from the database. wanted to do searches through a textbox with the data that are already in datagridview. type I type 'c' and everything that has c appears, regardless of which column it is. I am using windows form, bringing the data by layers. caught in the database the data like this:

public class ConsultarSaida
{
    public SqlDataAdapter dataAdapter = new SqlDataAdapter();
    public DataTable TabelaEntrada = new DataTable();

    public void GetData(SaidasDTO dados)
    {
       // EntradaDTO Informacoes = new EntradaDTO();

        try
        {
            // Specify a connection string. Replace the given value with a 
            // valid connection string for a Northwind SQL Server sample
            // database accessible to your system.
            string sql = "select vi.prestador_id,vi.tipo,vi.img3,pres.nome,pres.cpf,pres.rg from visita vi inner join prestador pres on pres.id = vi.prestador_id where vi.condominio_id = '" + Informacoes.Condominio_id + "' and vi.datahora_saida is null order by vi.id asc";


            // Create a new data adapter based on the specified query.
            dataAdapter = new SqlDataAdapter(sql, Conexao.obterConexao());

            // Create a command builder to generate SQL update, insert, and
            // delete commands based on selectCommand. These are used to
            // update the database.
            SqlCommandBuilder commandBuilder = new SqlCommandBuilder(dataAdapter);



            TabelaEntrada.Locale = System.Globalization.CultureInfo.InvariantCulture;
            dataAdapter.Fill(TabelaEntrada);

        }
        catch (SqlException e)
        {
            dados.mensagens = "Erro - Cliente " + e.Message;
        }
    }
}

and in the form thus:

 public void ConsultaVisita()
    {
        SaidasDTO dados = new SaidasDTO();
        ConsultarSaida consultarsaidas = new ConsultarSaida();

        //Chamar o método para gravar os dados acima
        consultarsaidas.GetData(dados);
        //consultarclientes.
        dgvSaida.Rows.Clear();

        foreach (DataRow linha in consultarsaidas.TabelaEntrada.Rows)
        {

            dgvSaida.Rows.Add(linha.ItemArray);
         }

        if (dgvSaida.SelectedRows.Count > 0)
        {
            int index = dgvSaida.SelectedRows[0].Index;

            if (index >= 0)
                dgvSaida.Rows[index].Selected = false;
        }
    }
  • You can filter the lines using the lines.Itemarray , converts the array into a string (string.Join(",", arr);) and then just adds to the lines that the string contains the key that you are looking for.

1 answer

0

Gustavo, I would change the way you work in two points:

First, change the way you work with the grid, don’t add the lines manually, use the Datagridview Datasource to populate it:

dgvSaida.DataSource = dados;

And second, to make the filter place your property SaidasDTO dados = new SaidasDTO(); global for the form (declare it out of the method) so you can use the Line to filter and then update the grid, example:

//--- Você pode fazer também no KeyPress de um TextBox por exemplo
private void BotaoProcurar_Click(object sender, EventArgs e)
{
     //---- Não conheço seu modelo, então essa parte do Where você trabalha da forma que quiser
     // essa parte você altera: "x.Nome.Contais(textBox.text)"
     var dadosFiltrados = dados.Where(x=>x.Nome.Contais(textBox.Text)).ToList();
     dgvSaida.DataSource = null;
     dgvSaida.DataSource = dadosFiltrados;
}

This way you will be able to search for data in memory. I hope I’ve helped.

  • the problem is that I already defined the name of each column and left some invisible, using dgvSaida.Datasource = data; it takes the name of the query column and leaves visible fields that n will be needed at that time

Browser other questions tagged

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