'How to get the result of a Linq into a combobox datasource?
On my form I have dropped an untyped dataset, and put some tables in it using the designer.
table SubFolder with columns SubFolderID, Category, Folder
table ProjectSubFolder with columns ProjectID, SubFolderID
Both table are populated by reading an XML file, and this all works as expected.
Now I need to set the datasource of a ComboBoBox to a list of SubFolders, joined with ProjectSubFolders, and for that I use the following linq
DataTable subfolders = dataSetProjects.Tables["SubFolder"];
DataTable projectSubFolders = dataSetProjects.Tables["ProjectSubFolder"];
var query =
from subFolder in subfolders.AsEnumerable()
join projectSubFolder in projectSubFolders.AsEnumerable()
on subFolder.Field<Int32>("SubFolderID") equals projectSubFolder.Field<Int32>("SubFolderID")
where projectSubFolder.Field<Int32>("ProjectID") == projectID
&& subFolder.Field<string>("Category") == "Models"
select subFolder;
and then I try to set the datasource of the combobox to this
comboBoxSubFolderModel.DataSource = query.ToList();
The result is that the combobox shows the correct number of rows, but with text System.Data.DataRow
So I tried to set the DisplayValue and ValueMember, but that keeps failing.
I tried
comboBoxSubFolderModel.DisplayMember = "subFolder.Folder";
comboBoxSubFolderModel.ValueMember = "subFolder.SubFolderID";
EXCEPTION Child list for field subFolder cannot be created
And also DisplayMember remains empty
Next I tried
comboBoxSubFolderModel.DisplayMember = "SubFolder.Folder";
comboBoxSubFolderModel.ValueMember = "SubFolder.SubFolderID";
EXCEPTION Child list for field SubFolder cannot be created
And again DisplayMember remains empty
Next attempt
comboBoxSubFolderModel.DisplayMember = "Folder";
comboBoxSubFolderModel.ValueMember = "SubFolderID";
EXCEPTION Cannot bind to the new display member; Parameter name: newDisplayMember
And again DisplayMember remains empty
So I took a look at the content of the variable query to see what is in it,
and it appears to me that query holds a table called SubFolder with the correct fields in it with the correct names.
So, how can I set the displayname and valuemember here ?
Maybe I should not do query.ToList() but than what should I use ?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|


