Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How do I use a bound combobox in a DataGridView?

Posted on 2007-08-08
13
Medium Priority
?
670 Views
Last Modified: 2013-11-05
Hi, I have a dataset with 2 tables:
Payment (PaymentId, Date, Description, Amount, PaymentMethodId)
PaymentMethod (PaymentMethodId, Description)

The first table contains details of any Payments that we receive and is displayed in the datagridview.
The second is a lookup table which lists the methods of payment (ie. cash, cheque, credit card)  and should appear as a combobox in the grid.

As you can see there is a foreign key (PaymentMethodId) between the two tables.

I'm binding the DataGridViewComboBoxColumn as follows:
comboboxColumn.DataSource = myDataset.PaymentMethod;
comboboxColumn.ValueMember = "PaymentMethodId";
comboboxColumn.DisplayMember = "Description";  

Then adding it to the grid.  But when the grid displays I receive the following data error "DataGridViewComboBoxCell value is not valid" and the cell value contains the Id (such as 1) rather than the Description (such as "Cash").

Any ideas what I'm doing wrong? Or does anyone know of any sample projects for this scenario?
Thanks in advance
0
Comment
Question by:otto8
12 Comments
 
LVL 7

Expert Comment

by:imateyelectronics
ID: 19656185
You have the wrong thing bounded, You want it to be PaymentMethodID.Description.  As long as you have your relationships set between the two tables this should work ok.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 19656205
The text portion of the cell needs to be in the list of drop down items.  Which field is the text portion bound to?

Bob
0
 

Author Comment

by:otto8
ID: 19660612
imateyelectronics:
Not sure I undesrtood your answer. Are you saying that I want my binding properties set up in a different order? ie...
comboboxColumn.ValueMember = "Description";
comboboxColumn.DisplayMember = "PaymentMethodId";  

Bob:
How can I bind the text portion of the cell? I've been tried setting it manually to use the code below but I still receive the same error.

foreach (DataGridViewRow row in gridInvPayments.Rows)
{
      DataGridViewComboBoxCell cell = (DataGridViewComboBoxCell)row.Cells["Dropdown"];
      cell.Value = Convert.ToInt16(row.Cells["PaymentMethodId"].Value);
}
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 96

Expert Comment

by:Bob Learned
ID: 19660900
If you are binding the DataGridView to a data source, did you set the DataPropertyName for the DataGridViewComboBoxColumn?

Bob
0
 

Author Comment

by:otto8
ID: 19661277
I've tried setting the DataPropertyName but am not sure if I'm passing it the correct value

            comboboxColumn.DataSource = currentJobDS.PaymentMethod;
            comboboxColumn.ValueMember = "PaymentMethodId"; // primary key of lookup tabke
            comboboxColumn.DisplayMember = "Description"; //name of lookup table display filed
            comboboxColumn.DataPropertyName = "PaymentMethodId"; //name of my datagridcolumn

Do u know of any code samples or example projects on this topic, because currently I'm finding it all a bit ambigous?
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 19662898
What is the DataGridView bound to?

Bob
0
 

Expert Comment

by:jlrray
ID: 19676230
Did you try binding the comboboxColumn.DataSource to a Binding Source rather than the DataSet?

I've had success doing this common task utilizing the designer to make the combobox lookup vs using solely code.
0
 

Author Comment

by:otto8
ID: 19690146
Bob: The datagridview is bound to a datatable (I can't bind it directly to the dataset because the data requires a fair amount of formatting before it is displayed). Is it the case that both the combo and the grid need to be bound to a dataset which contains an explicit relationship between the lookup table and the details table?

jlrray: Thanks I'll have a go at creating it using the designer
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 19691297
If you are bound to a DataTable, then what is the corresponding column for the ComboBox column?  The DataPropertyName would be that column name.

Bob
0
 

Author Comment

by:otto8
ID: 19799516
Still not able to get this to work!

Can someone please post a link or code for a working example of a bound datagridview with Combobox column (where the combobox contains values from a linked lookup table)?

If someone can demonstrate a working example I'm sure I can adapt it for my needs
0
 

Author Comment

by:otto8
ID: 19819810
0
 
LVL 1

Accepted Solution

by:
Computer101 earned 0 total points
ID: 19984788
PAQed with points refunded (500)

Computer101
EE Admin
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Screencast - Getting to Know the Pipeline
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question