DataGridView ComboBox column with databound item list

S

sklett

I'm changing from a DataGrid to a DataGridView and have run across a
problem. The items that are bound to the DataGrid have an int Property that
represents a primary key of a lookup table in my database. For example:

table JobTypes
1 | Manager
2 | Controller
3 | Supervisor

table Employee
2 | JobTypeID
(other fields)

I want to display the JobType name in the column of the datagridview, then
when a user clicks to edit the type I want to display a ComboBox that lists
all the records from the JobType table. When the user selects an item, I
want to store it's ID in the bound Employee object in the JobTypeID field.

I hope that makes sense. The part that I'm not clear on is how to bind the
combobox column to a separate datasource and have the ComboBox column select
the item that matches the value of JobTypeID.

Has anyone done this? Seems like it would be a common task but I haven't
any examples showing how to accomplish this.

Thanks for reading,
Steve
 
S

sklett

I've created a binding source and specified it in the DataGridView combo
column as the data source.
Now I can't specify the ValueMember and DisplayMember properties. I can
type in them, but when I move to the next property they are erased.

The DataSource for the new BindingSource is a simple DataTable.

wow... that modal dialog that the DataGridView throws up over and over and
over could drive a man crazy :0(
 
B

Brian Kelly

You need to bind the JobTypes datatable to your columns datasource.
Then set the DisplayMember and ValueMember properties. This will allow
Manager, Controller, etc to be displayed while their data
representation is 1, 2, etc... You then need to set the columns
datapropertyname to the "JobTypeID" column in the datagridview
datasource.

Put this in the Form_Load section of a form with a datagridview named
dataGridView1 for more clarity

dataGridView1.AutoGenerateColumns = false;

DataTable tableSource = new DataTable("tableSource");
tableSource.Columns.AddRange(new DataColumn[] {
new DataColumn("id"),
new DataColumn("job") });
tableSource.Rows.Add(1, "manager");
tableSource.Rows.Add(2, "supervisor");
tableSource.Rows.Add(3, "cashier");
tableGrid = new DataTable("tableGrid");
tableGrid.Columns.Add("jobid");
tableGrid.Rows.Add(2);

dataGridView1.DataSource = tableGrid;

DataGridViewComboBoxColumn col = new
DataGridViewComboBoxColumn();
col.DataSource = tableSource;
col.DisplayMember = "job";
col.ValueMember = "id";
col.DataPropertyName = "jobid";
dataGridView1.Columns.Add(col);
 
S

sklett

Hi Brian,

Thank you for the example code!
I was doing essentially the same thing, except I was using the designer. I
changed over to setting things up with code using my data sources and
property names and I'm still getting the same error:
"DataGridViewComboBoxCell value is not valid."

When I cut and paste your code, it works fine. I've checked the values of
my data source(s) and they seem correct.

So I looked a little closer and realized what the problem is; I have
mismatched types, my ValueMember is a byte and my DataPropertyName is an
int.

This is one of those times where a more detailed error message would be
great. It's also one of those times when I realize I've done something
stupid ;0)

Brian Kelly said:
You need to bind the JobTypes datatable to your columns datasource.
Then set the DisplayMember and ValueMember properties. This will allow
Manager, Controller, etc to be displayed while their data
representation is 1, 2, etc... You then need to set the columns
datapropertyname to the "JobTypeID" column in the datagridview
datasource.

Put this in the Form_Load section of a form with a datagridview named
dataGridView1 for more clarity

dataGridView1.AutoGenerateColumns = false;

DataTable tableSource = new DataTable("tableSource");
tableSource.Columns.AddRange(new DataColumn[] {
new DataColumn("id"),
new DataColumn("job") });
tableSource.Rows.Add(1, "manager");
tableSource.Rows.Add(2, "supervisor");
tableSource.Rows.Add(3, "cashier");
tableGrid = new DataTable("tableGrid");
tableGrid.Columns.Add("jobid");
tableGrid.Rows.Add(2);

dataGridView1.DataSource = tableGrid;

DataGridViewComboBoxColumn col = new
DataGridViewComboBoxColumn();
col.DataSource = tableSource;
col.DisplayMember = "job";
col.ValueMember = "id";
col.DataPropertyName = "jobid";
dataGridView1.Columns.Add(col);
I've created a binding source and specified it in the DataGridView combo
column as the data source.
Now I can't specify the ValueMember and DisplayMember properties. I can
type in them, but when I move to the next property they are erased.

The DataSource for the new BindingSource is a simple DataTable.

wow... that modal dialog that the DataGridView throws up over and over
and
over could drive a man crazy :0(
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top