Filtering in Bogotá

Asked

Viewed 473 times

2

Hello, I’m having a problem to perform filters on a DataGridView I am using Windows Forms and pulling information from a Mysql database to the DataGridView called dgvDados the information is being pulled perfectly, to carry out the filtering I have a ComboBox called cboBusca and a TextBox called txtCriterio where I would perform filtering so Ex:

cboBusca = "MANAGER" and txtCriterio = "João Paulo"

It’s a very simple filter to find a string follows below the code when clicked on the search button

private void btnProcurar_Click(object sender, EventArgs e)
    {
         DataTable table;
         table = new DataTable();
         dgvDados.DataSource = table;

            try
            {
                table.DefaultView.RowFilter = string.Format("" + cboBusca.Text + " like '%{0}%'", txtcriterio.Text.Replace("'", "''"));
                lblRegistros.Text = (dgvDados.Rows.Count - 1).ToString();
            }
            catch (Exception ex)
            {
                MetroFramework.MetroMessageBox.Show(this, "" + ex.Message, "Erro", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
    }

After clicking the button btnProcurar to carry out the search he says:

Unable to find the[MANAGER] column (in the case I selected on cboBusca)

In case of help I will leave the method code CarregarGrid() where in this case the bank information is pulled

public void CarregarGrid()
    {
        try{
            this.dgvDados.Rows.Clear();
            //indico o número de colunas
            dgvDados.ColumnCount = 14;
            objConnection = new MySqlConnection(caminho);
            //instância do comando onde passo
            //o sql e a conexão como parâmetro
            objComando =  new MySqlCommand("SELECT * FROM checagens" , objConnection);
            //abro a conexão
            objConnection.Open();
            //instâncio o leitor
            var leitor = objComando.ExecuteReader();
            //enquanto leitor está lendo

        while (leitor.Read())
        {
            //insiro os dados no dgvDados
            dgvDados.Rows.Add(leitor[0].ToString(),
                leitor[1].ToString(),
                leitor[2].ToString(),
                leitor[3].ToString(),
                leitor[4].ToString(),
                leitor[5].ToString(),
                leitor[6].ToString(),
                leitor[7].ToString(),
                leitor[8].ToString(),
                leitor[9].ToString(),
                leitor[10].ToString(),
                leitor[11].ToString(),
                leitor[12].ToString(),
                leitor[13].ToString());

        }
    }
        catch(Exception ex){
            MetroFramework.MetroMessageBox.Show(this, "" + ex.Message, "Erro", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }
        finally{
        //fecho conexão
        objConnection.Close();
        }
    }

I’m not carrying the information on Form_Load because it could get very overloaded so I created a Show All button and there I call the CarregarGrid()

private void btnExibirTodos_Click_1(object sender, EventArgs e)
    {
        CarregarGrid();
        dgvDados.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
        dgvDados.Columns[0].Name = "CNPJ";
        dgvDados.Columns[1].Name = "DATA";
        dgvDados.Columns[2].Name = "RAZÃO SOCIAL";
        dgvDados.Columns[3].Name = "OPERADORA";
        dgvDados.Columns[4].Name = "LINHAS";
        dgvDados.Columns[5].Name = "VIGENCIA";
        dgvDados.Columns[6].Name = "CONTRATO";
        dgvDados.Columns[7].Name = "VALOR GASTO";
        dgvDados.Columns[8].Name = "FIXO EMPRESA";
        dgvDados.Columns[9].Name = "GESTOR";
        dgvDados.Columns[10].Name = "CELULAR";
        dgvDados.Columns[11].Name = "FIXO GESTOR";
        dgvDados.Columns[12].Name = "EMAIL";
        dgvDados.Columns[13].Name = "OBSERVAÇÕES";

        lblRegistros.Text = (dgvDados.Rows.Count - 1).ToString();  
    }

1 answer

2

Look, I know I’m not answering your question. But I want to point out another way to your grid situation: look for the Syncfusion Community. It is a set of free components (take a look at the FAQ to see if your case applies) and it has a full-featured grid, among them an automatic filter line: the first line of the grid is not a given, it is a filter. Look at the link: https://help.syncfusion.com/windowsforms/sfdatagrid/filterrow

Browser other questions tagged

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