Populate a DataGrid with data from a list but informing which columns I want to display

1

Dear colleagues.

I want to find a Datagrid from a list. The detail is that I want to select the columns to be displayed. If you do not inhibit the auto-generation of the columns there is usually the data. But if you inhibit auto generation, it shows the created columns + the bank columns, but it does not display the data.

Here is the snippet of the routine:

  private void frmCFOPPesquisar_Load(object sender, EventArgs e)
    {
        dgvPesquisar.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;

        dgvPesquisar.AutoGenerateColumns = false;

        ConfiguraDGV();

        CarregarGrid();
    }

    public void ConfiguraDGV()
    {
        dgvPesquisar.Columns.Add("cfop", "CFOP");
        dgvPesquisar.Columns.Add("Descricao", "Descrição");
        dgvPesquisar.Columns.Add("NaturezaOperacao", "Natureza de Operação");            
    }

    public void CarregarGrid()
    {
            BancoContexto contexto = new BancoContexto();

            IEnumerable<Cfop> lista = from p in contexto.Cfops select p;

            dgvPesquisar.DataSource = lista.ToList();
    }

Thank you.

    
asked by anonymous 04.08.2017 / 22:19

1 answer

1

Set the dataproperty of the column:

dgvPesquisar.Columns["cfop"].DataPropertyName = "CFOP";
    
04.08.2017 / 23:23