Filling ComboBox Elements with elements from a dynamic database, C #

My database, db, has “Executor” as the primary key with the foreign key “CdTitle”, in one form the user can enter information to add to the database, in another form I have a combo box that I want to fill with the names artist in the database, primarily "Artist.Names". I tried using LINQ to move the database and put the query results in combobox, but it does not work as I thought.

The code I have is:

var ArtistNames = from name in formByArtist.db.Artists select name.Name; foreach (var element in ArtistNames) { comboBox1.Items.Add(element.ToString()); } 
+4
source share
2 answers

If your artist has a name and identifier, you can do this:

 comboBox1.DataValueField = "Id"; comboBox1.DataTextField = "Name"; comboBox1.DataSource = formByArtist.db.Artists; comboBox1.DataBind(); 
+1
source

From an existing sample, modify the following:

 var ArtistNames = (from name in formByArtist.db.Artists select name.Name) .ToList(); comboBox1.DataSource = ArtistNames; 
+1
source

Source: https://habr.com/ru/post/1347653/


All Articles