By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,678 Members | 2,439 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,678 IT Pros & Developers. It's quick & easy.

DataGridView ComboBox column with databound item list

P: n/a
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
Sep 1 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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(

"sklett" <sk****@mddirect.comwrote in message
news:ON**************@TK2MSFTNGP04.phx.gbl...
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

Sep 1 '06 #2

P: n/a
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);

sklett wrote:
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(

"sklett" <sk****@mddirect.comwrote in message
news:ON**************@TK2MSFTNGP04.phx.gbl...
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
Sep 2 '06 #3

P: n/a
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" <bj*****@gmail.comwrote in message
news:11**********************@i42g2000cwa.googlegr oups.com...
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);

sklett wrote:
>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(

"sklett" <sk****@mddirect.comwrote in message
news:ON**************@TK2MSFTNGP04.phx.gbl...
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

Sep 5 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.