Object is not an object instance in DataGridView

0

I'm trying to bring a SQL list and load it into a grid. After that, some fields of that grid must be of DataGridViewComboBoxColumn , so that the user can select options according to the combos. This is the code that I have made:

public async void ConfigurarGrilla()
{
        try
        {
            var responseArea = await servicio.AreaGetAllAsync();
            var responseEmpresa = await servicio.EmpresaGetAllAsync();

            if (responseArea.IsValid && responseEmpresa.IsValid)
            {
                listaArea = responseArea.Value;
                listaEmpresa = responseEmpresa.Value;

                dgvArticulos.AutoGenerateColumns = false;

                DataGridViewComboBoxColumn dtColumn = dgvArticulos.Columns["CodArea"] as DataGridViewComboBoxColumn;
                dtColumn.DataSource = listaArea;
                dtColumn.DisplayMember = "CodArea";

                DataGridViewComboBoxColumn dtColumn2 = (DataGridViewComboBoxColumn)dgvArticulos.Columns["RazonSocial"] as DataGridViewComboBoxColumn;
                dtColumn2.DataSource = listaEmpresa;
                dtColumn2.DisplayMember = "RazonSocial";

                LoadDocumento();
            }
            else
            {
                MessageBox.Show(responseArea.ErrorMensaje);
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show("Ocurrio un error " + ex.Message, "Aviso", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
}

The methods responseArea.Value and responseEmpresa.Value bring the list of the BD, which does contain information when I debug, but in the line dtColumn.DataSource = listaArea is where I get the error

  

"object is not an object instance"

I do not know what the error could be, since I have not left any objects without instantiating.

Will anyone know what the possible error is?

    
asked by Coeus 01.11.2016 в 22:44
source

1 answer

1

You need to indicate the ValueMember in both DataGridViewComboBoxColumn

DataGridViewComboBoxColumn dtColumn = dgvArticulos.Columns["CodArea"] as DataGridViewComboBoxColumn;
dtColumn.DataSource = listaArea;
dtColumn.DisplayMember = "CodArea";
dtColumn.ValueMember = "CodArea";

DataGridViewComboBoxColumn dtColumn2 = (DataGridViewComboBoxColumn)dgvArticulos.Columns["RazonSocial"] as DataGridViewComboBoxColumn;
dtColumn2.DataSource = listaEmpresa;
dtColumn2.DisplayMember = "RazonSocial";
dtColumn2.ValueMember = "RazonSocial";

I leave you a reference document DataGridView - Part 4 - Using the DataGridViewComboBoxColumns

    
answered by 01.11.2016 в 23:06