Populate combobox in datagridview

ssabc

Well-known member
Joined
Dec 11, 2017
Messages
63
Programming Experience
10+
Hello:

I must be off a little on this one.

Screen Shot 06-11-18 at 11.38 AM.PNG

Screen Shot 06-11-18 at 11.43 AM.PNG


Thanks!
 
You're selecting only the ProjectNumber string so dgvstrProjectNumbers is Ienumerable<string> (later ToList), and a string does not have 'ProjectNumber' property, no DisplayMember is needed for a string list.

In general when you set DisplayMember do so before assigning the data source.
 
Why are you setting the DataSource of each cell directly instead of just setting it for the column?

As JohnH suggests, the DisplayMember is supposed to be the name of a property or column in the data source or its items. It is the member of the items to be displayed instead of the result of calling ToString on the items. Your data source is a List<string> and neither that nor its items have a property or column named "ProjectNumber". If you were to bind the source DataTable, then it would make sense to set the DisplayMember, e.g.
comboBoxColumn.DisplayMember = "Name";
comboBoxColumn.ValueMember = "ID";
comboBoxColumn.DataSource = myDataTable;
 
No runtime errors, but no data either...

I have no runtime errors, but also no data, whatever have I forgotten?

C#:
                DataTable dt_Employee = timeDataSet.Tables["Employee"];
                DataTable dt_TimeData = timeDataSet.Tables["TimeData"];
                DataTable dt_Activity = timeDataSet.Tables["Activity"];
                DataTable dt_Jobs = timeDataSet.Tables["Jobs"];


                var dgvstrProjectNumbers = from a in dt_Jobs.AsEnumerable()
                                           orderby a.Field<String>("ProjectNumber")
                                           where (a.Field<String>("ProjectNumber") != null)
                                           select a.Field<String>("ProjectNumber");


                int i = 0;


                DataGridViewComboBoxColumn dgvcboProjectNumbers = (DataGridViewComboBoxColumn)(dgvTime.Columns[6]);
                dgvcboProjectNumbers.DataSource=dgvstrProjectNumbers.ToList();


No problem querying the data table:

Screen Shot 06-12-18 at 01.57 PM.PNG

Thanks!
 
Pretend that we know nothing about your project other than what you tell us... because that is the case. This:
but also no data
is just too vague. You are going through multiple steps so it's important to explain EXACTLY where things are different to what you expect. For a start, does dt_Jobs contain data? If not, what's the point of querying it? Don't just assume that it does. Debug your code and immediately before the LINQ query, test the Rows.Count property of that DataTable.
 
Yes, dt_Jobs contains data... Or should I say, the dataset contains data.
Screen Shot 06-13-18 at 08.24 AM.PNG

Screen Shot 06-13-18 at 08.26 AM.PNG

Do I have to do some kind of join query? T thought the dataset took care of these things with the defined relationships...

Thanks, I hope this makes more sense.

The ProjectNumber column needs to show up in the datagridviewcombobox...
 
I do not believe there is an issue with getting data at runtime because I am doing it in other places, and am able to easily populate other controls.

I believe the issue is in the syntax of populating a comboboxcolumn datasource in a datagridview, and I believe this syntax is wrong:

C#:
                DataGridViewComboBoxColumn dgvcboProjectNumbers = (DataGridViewComboBoxColumn)(dgvTime.Columns[6]);
                dgvcboProjectNumbers.DataSource = lstProjectNumbers;

Thanks
 
With regard to my earlier post, I have some data here, but I need to be able to assign some query values (as shown before), to these comboboxes!

Screen Shot 06-13-18 at 01.01 PM.PNG

From what I have read, I need to populate this information first, and then assign the tableadapterfill, is that correct?

C#:
                DataTable dt_Employee = timeDataSet.Tables["Employee"];
                DataTable dt_TimeData = timeDataSet.Tables["TimeData"];
                DataTable dt_Activity = timeDataSet.Tables["Activity"];
                DataTable dt_Jobs = timeDataSet.Tables["Jobs"];


                var qryProjectNumbers = from a in dt_Jobs.AsEnumerable()
                                        orderby a.Field<String>("ProjectNumber")
                                        where (a.Field<String>("ProjectNumber") != null)
                                        select a.Field<String>("ProjectNumber");


                var lstProjectNumbers = qryProjectNumbers.Distinct().ToList();


                // How do I add combobox columns?
                DataGridViewComboBoxColumn dgvcboProjectNumbers = (DataGridViewComboBoxColumn)(dgvTime.Columns[6]);
                dgvcboProjectNumbers.DataSource = lstProjectNumbers;


                this.timeDataTableAdapter1.Fill(this.timeDataSet.TimeData);

When I run the code, I also get this error, though the data for these fields is Nothing. I assume I need to program against null. However, there will be queries defining what this data should be.

Screen Shot 06-13-18 at 01.10 PM.PNG
 
Last edited:
Why is something so simple, so time consuming?

Sorry for my rant, but I have been spending about a full week trying to figure out how to write three lines of code.

It is great that ...

C#:
[COLOR=#333333][FONT=Consolas]comboBoxColumn.DisplayMember = "Name";[/FONT][/COLOR]
[COLOR=#333333][FONT=Consolas]comboBoxColumn.ValueMember = "ID";[/FONT][/COLOR]
[COLOR=#333333][FONT=Consolas, Bitstream Vera Sans Mono, Courier New, Courier, monospace]comboBoxColumn.DataSource = myDataTable;


[/FONT]

[FONT=Verdana, Arial, Tahoma, Calibri, Geneva, sans-serif]But I am having a hard time relating this to the variable of ProjectNumber. I am also wondering where ID even comes in to play. It was my hope to do a query, connvert it to a list, and let those values be the values for by combobox datasource.

Note, in my online searching, this is a common question, but the answers are few.

It does seem that I could just go to my datagridview properties and select columns, and then pan to the ProjectNumber column, which is already a combobox. In there are:

Datasource, DisplayMember and ValueMamber, all None. The Items says Collection, but there is no data there. Wouldnt it just be cool if I could add my query list to the collection here?

I toyed around with changing the datasources, but nothing helped. The fact is, the tables are all setup in the TimeData dataset, pictured in all of my prior posts.

I am so sorry I am not seasoned at this, as I have never done it before. But I'm not so sure it's rocket science. What it shouold be is three or four lines of code. I do believe my query wil work.

What is the ValueMember?

What is the DisplayMember?

I have seen these things identified online, and some people thing one is the other and the other is the one, or vice versa.


Sorry if I have not been clear on this post. I have tried to include as much information as possible.

Thanks!





[/FONT][/COLOR]
 
Back
Top Bottom