DataGridView Combobox Column
-
Wednesday, September 19, 2012 7:35 AM
C# VS2010 SS2k8 .Net 4
I'm developing a rather simple form to browse Work Orders. Choose a Project, show all the (16 or so) work orders that are assigned to that project. Or, choose a discipline and show all the (thousands) of work orders assigned to that discipline (Piping, Electrical, etc.) Production DB, read-only form, very simple....
I set up the DataGridView, using an EF model as the datasource (pretty much all wizzard-based.) Then, I changed the "WODisciplineID" column to be a combo box, and used the wizzard to select the EF model for "Disciplines" and selecting a value ("ID") and display item ("Discipline"), again, with the wizzard. Seems it would be a no-brainer. Now, the DGV should show the discipline description instead of the discipline ID.
When I run it, it compiles, but fires the DATA_Error event of the DGV on every row, then leaves the column saying "None". I checked, and the WODisciplineID is always valid (guaranteed by referential integrity.)
What am I doing wrong?
Jim
All Replies
-
Wednesday, September 19, 2012 3:44 PM
Suggestion:
why you dont do your own code, instead of some "automatically-generated-code-by-C#"? You will sooner or later encounter problems.
You know what is the worst part here: you actually dont have any code to show to us, and we are unable to help you (we can only guess or suggest you a bit, but thats about it). There is the code written in some class for what you did in the designer, so you have to look for it, else we cannot do much, becuase its hard to say what can be the reason for these issues of yours now.
---
So, I am really telling you, start doing your own code, writing it, it will take a bit more of a time from the beginning, but at least we can be able to help you if you will encounter on problems.
So to maybe help you a bit more, you can tell me some more:
- what datasouce do you use for datagridview?
- what datasouce do you use for comboBox?
- how did yo create comboBox column (because you have to create before populating cells)?
- do you use same data in each of comboBoxCells or each has its own data?
- if you have any code, please show it to us, the code that might be helpful to salve the issue.
Mitja
-
Thursday, September 20, 2012 5:33 AM
Mitja, I appreciate your position, but populating 5 properties in a DGV which will never change at run time should be done at design time, not run time.
Not alot of code:
using System; using CPASEntityFramework; using System.Data.Entity; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace BrowseWorkOrders { public partial class BrowseWOs : Form { public BrowseWOs() { InitializeComponent(); } private void BrowseWOs_Load(object sender, EventArgs e) { // Following loads up all Projects into the cbProjectID Combo Box var context = new CPASEntities(); var PrID = context.qryProjectIDNbrDescs.ToList(); cbProjectID.ValueMember = "ID"; cbProjectID.DisplayMember = "ProjectNbr"; cbProjectID.DataSource = PrID; // Following loads up all Disciplines into the cbDisciplineID Combo Box var DiscID = context.tblDisciplines.ToList(); cbDisciplineID.ValueMember = "ID"; cbDisciplineID.DisplayMember = "Discipline"; cbDisciplineID.DataSource = DiscID; } private void cbProjectID_SelectedIndexChanged(object sender, EventArgs e) { // When the user selects a new Project, filter the Datagridview to reflect his choice... var context = new CPASEntities(); var query = context.tblWorkOrders.Where(c => c.ProjectID == (int)cbProjectID.SelectedValue).OrderBy(d => d.WONbr).ToList(); tblWorkOrderBindingSource.DataSource = query; } private void tblWorkOrderDataGridView_DataError(object sender, DataGridViewDataErrorEventArgs e) { } private void tblDisciplineBindingSource_CurrentChanged(object sender, EventArgs e) { } private void tblWorkOrderBindingSource_CurrentChanged(object sender, EventArgs e) { } } }Here's the sequence I followed:
- Created the EF model as a class (so it can be reused) following Julie Lerner's excellent videos.
- Dragged an object ("tblWorkOrders") onto a new Winforms form, automatically creating columns (text box columns) for each of the columns from the EF model.
- Dropped the columns I didn't need.
- Tested the DGV -- works fine.
- Edited the "WODisciplineID" column, changing it to a combo box instead of a text box.
- Set the Datasource property to another EF table object ("tblDiscipline")
- Set the Value to "ID" (the primary key from the "tblDiscipline" object of which WODisciplineID is a FK)
- Set the Display to "Discipline" (the description field I want displayed)
- Tested it, but the DATAError event fired on every row, and until I created an event handler, the error message said the data was not valid, I'm assuming "ID" from the "tblWorkOrders" object. I added an event handler that does nothing, and the combo box shows up with the word "None" in it. (This is a production DB, and WODisciplineID is NOT NULL, and referential integrity guarantees a valid field.
- The datasource for the column comes from the same EF model as the DGV.
Here is a look at what I've done with that column in specific:
And here's the form when executed ("None" is not one of the available disciplines):
Jim
-
Monday, September 24, 2012 11:15 AMModerator
Hi Jim,
You need to set ValueMember to the value of DataPropertyName.
Best Regards,
Bob Wu [MSFT]
MSDN Community Support | Feedback to us
-
Wednesday, September 26, 2012 7:53 AM
Bob, I've racked my brains, but I can't think what your answer means. In design mode, I set the DGV's source to the work order object. The individual column is set to a property of that EF object. Then, I change the discipline column to a combo box and set the combo box's source to the discipline table EF object. The designer then gives me a choice for Value Member, and I choose "ID", which is the primary key of the discipline table. What's "DataPropertyName"?
Now, it dawns on me that the combo box on the side of the form labelled "architectural" has the same datasource object. Perhaps that's the problem. Do I need to create a new data source for the DGV column in order for them to be independent? Or do I need to assign it at run time with a list object?
Help me out here, Bob....
Jim
Jim
- Edited by JimS-Indy Wednesday, September 26, 2012 7:55 AM
-
Wednesday, September 26, 2012 11:49 AMModerator
Hi Jim,
I'm sorry that my previous reply is not clear enough and it is not well explained.
DataPropertyName is a property of each DataGridViewColumn, see http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcolumn.datapropertyname.aspx.
Base on my knowledge, the value of DataGridViewComboBoxCell must be legal. It's value must be a member of the drop-down list. According to your description, I think the value your current get is not a member of the list.
As far as I know, the list of a data bound DataGridViewComboBoxColumn is determined by it's DataGridViewComboBoxColumn.DataSource Property and DataGridViewComboBoxColumn.ValueMember Property. On the other hand, the current cell value, no matter what type of column it belongs, is depended on the DataGridView.DataSource Property and DataGridViewColumn.DataPropertyName Property. As the MSDN document,each column (automatically) sets its DataPropertyName property to the name of a property or database column in the data source specified by the DataSource property.
If you check the DataGridViewTextBoxColumn in your code, you will found the DataPropertyName property is set in the designer file.
Normally, when one table is set to a datagridview' datasource and the other table is set to the datasource of the combobox column, both ValueMember and DataPropertyName will set to the forget key(or primary key of the other table).
I hope this is clear and sorry for the inappropriate explain in my previous reply.
I wrote a example with NorthWind, you can download it through SkyDrive.
Best Regards,
Bob Wu [MSFT]
MSDN Community Support | Feedback to us
- Edited by Bob Wu-MTMicrosoft Contingent Staff, Moderator Wednesday, September 26, 2012 11:50 AM
- Marked As Answer by Mike Dos ZhangMicrosoft Contingent Staff, Moderator Tuesday, October 09, 2012 9:16 AM
-
Tuesday, October 09, 2012 1:53 PMI have to admit, I gave up on this. I rewrote the query to obviate the need for a combo box. I'm sure I'll run into it again, but for now, thank you, Bob et al for your help. I do appreciate it.
Jim


