ComboBox in DataGridView

4

I work in a Windows Forms app in which I have a DataGridView control in which one of its columns is of the Combobox type, I can populate the ComboBox without any problem.

The problem I have is that I need the first element that contains the Combobox selected in this case is <<Seleccione>> I could not get it at first you do not see anything in the ComboBox until you click to deploy the ComboBox and be able to select the items.

This way I charge the ComboBox

public IEnumerable<UniversalExtend> SelectList(Expression<Func<UnidadMedida, UniversalExtend>> source)
    {
        return _unidadMedidaRepository.SelectList(source);
    }

    public IEnumerable<UniversalExtend> ListaUnidadMedidas
        (Expression<Func<UnidadMedida, UniversalExtend>> source)
    {
        var listaItem = SelectList(source).ToList();
        listaItem.Insert(0, new UniversalExtend() { Id = -1, Descripcion = "<<<Seleccione>>>" });
        return listaItem;
    }

Persistence

public IEnumerable<UniversalExtend> SelectList(Expression<Func<T, UniversalExtend>> source)
    {
        using (var context = new BusinessContext())
        {
            var result = context.Set<T>().AsNoTracking()
                .Select(source).ToList();
            return result;
        }
    }

Linking the data to the ComboBox

dataGridView1.AutoGenerateColumns = false;
        DataGridViewComboBoxColumn cboColMedida = dataGridView1.Columns["colCombo"] as DataGridViewComboBoxColumn;
        cboColMedida.DataSource =
            _saUnidadMedida.ListaUnidadMedidas(
                x => new UniversalExtend() {Id = x.UnidadMedidaId, Descripcion = x.Abreviacion}).ToList();
        cboColMedida.DisplayMember = "Descripcion";
        cboColMedida.ValueMember = "Id";

This is how it is shown when the cbo is already loaded with data.

    
asked by Pedro Ávila 20.07.2018 в 03:17
source

2 answers

3

I have been playing with your code and the answer is quite simple enough to assign a value to the cell of the combobox (In your example give the value -1 that you declared in the Id) just after linking the data with your combobox.

dataGridView1.Rows[0].Cells["colCombo"].Value =-1;

That is, in the part of the code where you assign the DataSource, it would look like this:

    dataGridView1.AutoGenerateColumns = false;
    DataGridViewComboBoxColumn cboColMedida = dataGridView1.Columns["colCombo"] as DataGridViewComboBoxColumn;
    cboColMedida.DataSource =
            _saUnidadMedida.ListaUnidadMedidas(
                x => new UniversalExtend() {Id = x.UnidadMedidaId, Descripcion = x.Abreviacion}).ToList();
    cboColMedida.DisplayMember = "Descripcion";
    cboColMedida.ValueMember = "Id";
    dataGridView1.Rows[0].Cells["colCombo"].Value =-1;
    dataGridView1.RowsAdded += DataGridView1OnRowsAdded;

And the function that will make the selection each time a new row is added would be:

private void DataGridView1OnRowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
    {
        dataGridView1.Rows[e.RowIndex].Cells["colCombo"].Value = -1;
    }

I hope you can solve the problem.

Greetings

    
answered by 26.07.2018 / 10:05
source
0

What you need is to capture the default null value of the column in the combobox:

cboColMedida.DefaultCellStyle.NullValue = "<<<Seleccione>>>";

This after having finished filling the data to the DataGridViewComboBoxColumn

    
answered by 26.07.2018 в 01:21