473,581 Members | 2,903 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 94233
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****@mddirec t.comwrote in message
news:ON******** ******@TK2MSFTN GP04.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
datapropertynam e 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.A utoGenerateColu mns = false;

DataTable tableSource = new DataTable("tabl eSource");
tableSource.Col umns.AddRange(n ew DataColumn[] {
new DataColumn("id" ),
new DataColumn("job ") });
tableSource.Row s.Add(1, "manager");
tableSource.Row s.Add(2, "supervisor ");
tableSource.Row s.Add(3, "cashier");
tableGrid = new DataTable("tabl eGrid");
tableGrid.Colum ns.Add("jobid") ;
tableGrid.Rows. Add(2);

dataGridView1.D ataSource = tableGrid;

DataGridViewCom boBoxColumn col = new
DataGridViewCom boBoxColumn();
col.DataSource = tableSource;
col.DisplayMemb er = "job";
col.ValueMember = "id";
col.DataPropert yName = "jobid";
dataGridView1.C olumns.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****@mddirec t.comwrote in message
news:ON******** ******@TK2MSFTN GP04.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:
"DataGridViewCo mboBoxCell 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 DataPropertyNam e 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.goo glegroups.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
datapropertynam e 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.A utoGenerateColu mns = false;

DataTable tableSource = new DataTable("tabl eSource");
tableSource.Col umns.AddRange(n ew DataColumn[] {
new DataColumn("id" ),
new DataColumn("job ") });
tableSource.Row s.Add(1, "manager");
tableSource.Row s.Add(2, "supervisor ");
tableSource.Row s.Add(3, "cashier");
tableGrid = new DataTable("tabl eGrid");
tableGrid.Colum ns.Add("jobid") ;
tableGrid.Rows. Add(2);

dataGridView1.D ataSource = tableGrid;

DataGridViewCom boBoxColumn col = new
DataGridViewCom boBoxColumn();
col.DataSource = tableSource;
col.DisplayMemb er = "job";
col.ValueMember = "id";
col.DataPropert yName = "jobid";
dataGridView1.C olumns.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****@mddirec t.comwrote in message
news:ON******* *******@TK2MSFT NGP04.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
16332
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
9641
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 = (DataGridViewComboBoxCell) row.Cells; for (int i = 1; i < 51; i++) { cell.Items.Add(i);
1
2800
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 runtime after the Datagridview is bound? *** Sent via Developersdex http://www.developersdex.com ***
1
1698
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 "product A" only has "Suplier B", "Suplier D" and "Suplier E", "Product B" only has "Suplier A", "Suplier C", and "Product C" only has "Suplier B",...
3
43753
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 created a combobox column in the datagridview beside the holiday checkbox so when the dataentry check the holiday checkbox he can choose what type of...
0
1473
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 OnEditingControlShowing handler and setting the DropDownStyle to ComobBoxStyle.DropDown: protected override void...
0
1683
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 'Category' column of tblItems. Now in the 'Category' column of the datagridview I want to set the value of a certain cell to Null programmatically....
1
1985
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 10000 records in it so now when i want to select a professor from that list I click on the dropdown arrow of the combobox column but the dropdown...
0
2843
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 details in data grid view.
0
7808
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8158
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8312
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7917
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
5366
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3835
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2311
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1413
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1147
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.