I have the same scenario like this one and below is the solution that I did.
I have my datasource in datagridview is TestEquipment.
public classTestEquipment
{
public string Name {get;set;}
public string Command {get;set;}
public TestEquipmentTypeTEquipmentType{get;set;}
}
and my datasource in my DataGridViewComboBoxColumn is TestEquipmentType
public classTestEquipmentType
{
public int Id {get;set;}
public string Description {get;set;}
}
I bind both in my initialization..
//My datagridView has 4 columns, instead of 3. My 3rd column is bind to theDataGridViewComboBoxColumnwhile the 4th column is bind to the datasource of the //whole datagridview.
dgridViewTestStationEntry.AutoGenerateColumns = false;
dgridViewTestStationEntry.Columns[(int)DgridTestEquipmentEntryColumns.Name].DataPropertyName = "Name";
dgridViewTestStationEntry.Columns[(int)DgridTestEquipmentEntryColumns.Command].DataPropertyName = "Command";
dgridViewTestStationEntry.Columns[(int)DgridTestEquipmentEntryColumns.TestEquipmentTypeObject].DataPropertyName = "TEquipmentType"; //this column is visible =false, this is my 4th column
BindingSource bsTestEquipment = new BindingSource();
bsTestEquipment= GetActiveTestEquipment(); //Grabbing the data
dgridViewTestStationEntry.DataSource =bsTestEquipment;
//Bind yourDataGridViewComboBoxColumn which is colType in this case
colType.DisplayMember = "Description";
colType.ValueMember = "Id";
colType.DataPropertyName = "Id";
BindingSourcebsTestEquipmentType= new BindingSource();
bsTestEquipmentType.DataSource =GetTestEquipmentTypes(); //Contains your choices
colType.DataSource =bsTestEquipmentType;
//To make sure that you are getting the updated selected item fromDataGridViewComboBoxColumn
private void dgridViewTestStationEntry_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
if (dgridViewTestStationEntry.CurrentCell.ColumnIndex == colType.Index)
{
DataGridViewComboBoxEditingControl comboBox = e.Control as DataGridViewComboBoxEditingControl;
BindingSource bindingSource = new BindingSource();
bindingSource = (BindingSource)comboBox.DataSource;
dgridViewTestStationEntry.CurrentRow.Cells[(Int32)DgridTestEquipmentEntryColumns.TestEquipmentTypeObject].Value = bindingSource.Current;
comboBox.SelectionChangeCommitted -= this.comboBox_SelectionChangeCommitted;
comboBox.SelectionChangeCommitted += this.comboBox_SelectionChangeCommitted;
}
}
void comboBox_SelectionChangeCommitted(object sender, EventArgs e)
{
this.dgridViewTestStationEntry.EndEdit();
}
-->I am not really happy with this but it works for now so if anyone has a better solution please let me know. I would be more than happy to review it.