473,385 Members | 1,185 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

DataGridView ComboBox column with databound item list

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
3 94177
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Nathan | last post by:
Hi, I have a datagridview bound to a List of objects (ObjectA). Each ObjectA contains an ObjectB property. Class ObjectA { public ObjectB objB {} }
2
by: Greg | last post by:
I've populated all the cells in a combo box column with values from 1 - 50 using the following code: foreach (DataGridViewRow row in uxTabSummary.Rows) { DataGridViewComboBoxCell cell =...
1
by: Terry Olsen | last post by:
I have a Datagridview that is bound to a DataTable. So there are no column templates to modify at design time. Is there a way I can convert a certain column (if it exists) to a ComboBoxColumn at...
1
by: Bhavanimikkilineni | last post by:
H, At this moment my problem is how to make suplier combobox has different list in every row. I want suplier lists that related with a certain product instead of providing all the supliers. Let say...
3
by: Motawee | last post by:
i am making a windows form application that contains a datagridview that is filled with employees names the program is for the attendance absence holidays of employees in the company so i...
0
by: Chris Bray | last post by:
I have a data grid view which works very well in almost all respects. However, I have a problem that arises when I allow the addition of items to the grid on the fly. I do this by intercepting the...
0
by: priyamtheone | last post by:
There's an editable datagridview populated from a table say tblItems. Among the columns of the datagridview there's a combobox column named 'Category'. This column is populated by the respective...
1
by: Hardy123 | last post by:
Hi, I am having a combobox column in a datagridview named "dgcProfessor" which contains the list of professors names. i am filling that column with a datatable Now that datatable have more than...
0
by: visweswaran2830 | last post by:
Hi, I am having datagridview in that I have datagridview combobox column and noraml textfield. I want to know that how can I load selected value in that combox box when I am loading all entered...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.