How to hide column of DataGridView?

3

I have the void PopulaGrid(DataGridView grid, SQLiteDataReader dados) function that feeds my dataGridViewAlunos data, 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 the code of the PopulaGrid function:

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 is my function that returns a SQLiteDataReader to feed 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;
}
    
asked by anonymous 13.12.2015 / 02:23

2 answers

2

The solution to hide the desired column that is the id_aluno and use the Visible property of the column, follows the resolved code:

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.
}
    
13.12.2015 / 14:50
2

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

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);
    }
}

And you have to do this for SQL:

"id_aluno AS 'Código', " +

I do not know if you have other points, but you have already understood what to do to solve everything.

This code is insecure and has architectural problems. But this is already beyond the scope of the question.

    
13.12.2015 / 05:00