473,700 Members | 2,845 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 94257
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
16337
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
9643
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
2811
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
1710
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", "Suplier D". I can provide store procedure for this, but I don't know how to put this different...
3
43820
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 holiday is this to subtract it from the employee holiday counter i need to fill this combobox column...
0
1476
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 OnEditingControlShowing(DataGridViewEditingControlShowingEventArgs e) { cb = e.Control as ComboBox; ...
0
1692
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. Suppose when I press keyboard Delete button over the cell or some other button on the form, the value of...
1
1993
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 list takes some time to display the list of professors on the screen, so it is not displaying in...
0
2847
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
8709
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9202
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9058
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8909
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6555
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4395
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4649
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3081
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
3
2018
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.