Guillermo
Guillermo

Reputation: 213

Setting the Selected Value on DatagridView.ComboboxColumn

I am currently trying to complete a form with a grid that is loaded programmatically.

The grid has 6 columns and the last column is a ComboBox

This is part of the code

foreach(var persona in asistenciaRepo.FilterBy(x => x.plaserv == planilla).OrderBy(x => x.orden))
{
  grilla_personal.Rows.Add(persona.personal.id, persona.personal.id_legajo, persona.jerarquia.descripcion, persona.personal.nomyape, persona.orden, persona.codificacion.descripcion);
}

Basically what I'm trying is that when the grid is loaded and displays the selection combobox stored in my entity

the persona.codificacion.descripcion is the column of my entity that contains the data you wish to link to my control.

It is necessary to handle the event SelectedValueChanged or what is the way to do it correctly.

[UPDATE]

Column 6 is loaded programmatically

foreach (var c in codifRepo.GetAll())
{
   codificacion.Items.Add(c);
}
codificacion.ValueMember = "id";
codificacion.DisplayMember = "descripcion";

codificacion is the name of the ComboBoxColumn

Upvotes: 0

Views: 1681

Answers (1)

David Hall
David Hall

Reputation: 33143

Your DataGridViewComboBoxColumn needs to have its DataPropertyName property set.

This tells the column what property in the DataGridView's datasource to use to get each comboboxes selected value.

You will also need to have provided a DataSource for the combobox column itself, which specifies the list of values in the drop down.

The columns ValueMember and DisplayMember properties need to be set too. ValueMember is the property in the column's datasource that matches the DataPropertyName property. DisplayMember is the property to display to users.


If you aren't using a datasource for the grid, I'd strongly recommend it - add each row's values to a BindingList and then set that as the grid's datasource.

Upvotes: 1

Related Questions