Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Using DataGridViewComboBoxColumn.DataSource in a databound DataGridView (with EF)
 

Using DataGridViewComboBoxColumn.DataSource in a databound DataGridView (with EF)

Hi,

I'm binding a DataGridView to an ObjectResult<T> which works so far. Now I want one of my columns in my view to be a combobox and offer a selection of possible values (specific rows from another table in the same database - the column in question is a foreign key) but it must still be bound to the original QueryResult. I.e. when I change my selected item it should update the row. How can I achieve this?

I'm crossposting this because I'm not sure which one is the right forum for this (don't know if there are EF specific things to be aware of).

Thanks for any hint!
ctusch  Wednesday, September 23, 2009 4:29 PM
Hey,

thanks for your reply, it certainly is a working solution. But I have found a solution that's a bit more elegant and I'm using it now. You can find the post here:
http://groups.google.com/group/microsoft.public.dotnet.framework.windowsforms/msg/442fa4849f0c3092

Just to repeat the solution:

Add a property that returns the instance to the class you want to use as source for the combobox items and then set the ValueMember to it.
Set the DisplayMember to the property you want (it has to be set).
The DataPropertyName is set to the column/property from the data source of the datagridview from which you want the actual value to come from.

Here is the code I'm using to set up the combobox:

DataGridViewComboBoxColumn typeColumn = new DataGridViewComboBoxColumn()
{
    HeaderText = "Type",
    DataPropertyName = "Type",
    DataSource = entities.Type.Execute(MergeOption.AppendOnly),
    ValueMember = "Instance",
    DisplayMember = "Name"
}; 
Note that 'Type' refers to the table with all possible values in the DataSource line. The other two occurrences are referring to the column in the main table.
  • Marked As Answer byctusch Tuesday, September 29, 2009 9:14 AM
  •  
ctusch  Tuesday, September 29, 2009 9:14 AM
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.


  • Marked As Answer byAland LiMSFT, ModeratorFriday, September 25, 2009 5:30 AM
  • Unmarked As Answer byctusch Tuesday, September 29, 2009 8:54 AM
  • Proposed As Answer bya-caridad- Wednesday, September 23, 2009 9:01 PM
  •  
a-caridad-  Wednesday, September 23, 2009 8:59 PM
Hey,

thanks for your reply, it certainly is a working solution. But I have found a solution that's a bit more elegant and I'm using it now. You can find the post here:
http://groups.google.com/group/microsoft.public.dotnet.framework.windowsforms/msg/442fa4849f0c3092

Just to repeat the solution:

Add a property that returns the instance to the class you want to use as source for the combobox items and then set the ValueMember to it.
Set the DisplayMember to the property you want (it has to be set).
The DataPropertyName is set to the column/property from the data source of the datagridview from which you want the actual value to come from.

Here is the code I'm using to set up the combobox:

DataGridViewComboBoxColumn typeColumn = new DataGridViewComboBoxColumn()
{
    HeaderText = "Type",
    DataPropertyName = "Type",
    DataSource = entities.Type.Execute(MergeOption.AppendOnly),
    ValueMember = "Instance",
    DisplayMember = "Name"
}; 
Note that 'Type' refers to the table with all possible values in the DataSource line. The other two occurrences are referring to the column in the main table.
  • Marked As Answer byctusch Tuesday, September 29, 2009 9:14 AM
  •  
ctusch  Tuesday, September 29, 2009 9:14 AM

You can use google to search for other answers

Custom Search

More Threads

• datagridviewcomboboxcell with different values in each row
• BindingComplete event isn't firing - why?
• Unable to reference child table in datagrid.
• Problem With Adding a DataGridViewBoxCell into a Data Table
• First visible row number
• Export to Excel Template
• Datagridview Footer
• How to jump down to first letter you type in Datagridview?
• Lookup where is the primary key of a foreign key
• error in DataGridViewComboBoxCell