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.