How to hide Datagridview column?

Asked

Viewed 1,914 times

3

I have the function void PopulaGrid(DataGridView grid, SQLiteDataReader dados) that feeds data to my dataGridViewAlunos, I would like to know how to hide the first column of dataGridViewAlunos which is the column corresponding to the student id (id_aluno), below follows the code of the function PopulaGrid:

void PopulaGrid(DataGridView grid, SQLiteDataReader dados) 
{
    grid.Rows.Clear();
    grid.Columns.Clear();

    for (int i = 0; i < dados.FieldCount; i++) 
    {
        DataGridViewColumn coluna = new DataGridViewTextBoxColumn();

        coluna.HeaderText = dados.GetName(i);
        coluna.Visible = true;
        coluna.Name = "coluna" + 1;
        coluna.Resizable = DataGridViewTriState.True;
        grid.Columns.Add(coluna);
    }

    while (dados.Read()) 
    {
        object[] campos = new object[dados.FieldCount];

        for (int i = 0; i < dados.FieldCount; i++)
            campos[i] = dados.GetValue(i);

        grid.Rows.Add(campos);
    }
}

Below follows my function which returns a SQLiteDataReader to power the grid:

SQLiteDataReader FiltrarAlunos(string nome) 
{
    SQLiteDataReader dados = null;

    try
    {

        string query = "SELECT " +
                                "id_aluno AS 'Código', " +
                                "nome AS 'Nome', " +
                                "data_cadastro AS 'Data do Cadastro', " +
                                "telefone AS 'Telefone', " +
                                "celular AS 'Celular', " +
                                "endereco AS 'Endereço', " +
                                "observacao AS 'Observação', " +
                                "email AS 'E-Mail' " +
                                "FROM Alunos ";

        if (!string.IsNullOrEmpty(nome))
            query += "WHERE nome LIKE '%" + nome + "%'";

        DadosConexao dados_conexao = new DadosConexao();

        SQLiteConnection conexao = (new DALConexao(dados_conexao.String_Conexao).Conexao);
        conexao.Open();

        SQLiteCommand command = conexao.CreateCommand();
        command.CommandText = query;

        dados = command.ExecuteReader();
    }
    catch (Exception ex) 
    {
        MessageBox.Show(ex.Message);               
    }

    return dados;
}

2 answers

2

If it is guaranteed that it is the first column, it is very easy, just start with the second:

void PopulaGrid(DataGridView grid, SQLiteDataReader dados) {
    grid.Rows.Clear();
    grid.Columns.Clear();
    for (int i = 1; i < dados.FieldCount; i++) { // <========================== mudei aqui
        DataGridViewColumn coluna = new DataGridViewTextBoxColumn();
        coluna.HeaderText = dados.GetName(i);
        coluna.Visible = true;
        coluna.Name = "coluna" + 1;
        coluna.Resizable = DataGridViewTriState.True;
        grid.Columns.Add(coluna);
    }
    while (dados.Read()) {
        object[] campos = new object[dados.FieldCount];
        for (int i = 1; i < dados.FieldCount; i++) // <========================= mudei aqui
            campos[i] = dados.GetValue(i);
        grid.Rows.Add(campos);
    }
}

I put in the Github for future reference.

And you have to get this from SQL:

"id_aluno AS 'Código', " +

I don’t know if there are other points, but you already understand what to do to solve everything.

This code is insecure and has architecture problems. But this is already outside the scope of the question.

  • He hid the first column put in the second column which is the nome is the content of the id field (id_aluno) and the contents of the column name went to the third column.

  • If you want I edit the question and put my sql?

  • I forgot to change underneath.

  • I made the change, hid the column id, but the column name was empty this time, and its contents went to the adjoining columns, and with the other columns also, their respective contents went to the column forward.

  • You probably have to change it at other points in the code as well. Since you only have this bit, I can only help you so far. But you have already picked up where the problem is, just see the other places where you need to use similar technique. Everything has to be shifted in a column.

  • I added more information to the question.

  • I will reformulate the query routine, I have already solved here.

  • I’ll post as an answer.

Show 3 more comments

2


The solution to hide the desired column which is the id_aluno and use the property Visible column, follows the code solved:

void PopulaGrid(DataGridView grid, SQLiteDataReader dados) 
{
    grid.Rows.Clear();
    grid.Columns.Clear();

    for (int i = 0; i < dados.FieldCount; i++) 
    {
        DataGridViewColumn coluna = new DataGridViewTextBoxColumn();

        coluna.HeaderText = dados.GetName(i);
        coluna.Visible = true;
        coluna.Name = "coluna" + 1;
        coluna.Resizable = DataGridViewTriState.True;
        grid.Columns.Add(coluna);
    }

    while (dados.Read()) 
    {
        object[] campos = new object[dados.FieldCount];

        for (int i = 0; i < dados.FieldCount; i++)
            campos[i] = dados.GetValue(i);

        grid.Rows.Add(campos);
    }

    grid.Columns[0].Visible = false; //Mudei a propriedade Visible da coluna 0 que é a coluna id_aluno.
}

Browser other questions tagged

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