I have this form

enter image description here

When you click the "Add Author" button, the following form opens with a 4 textbox . The name of the added author is entered in the listBox1 on Form1 , when you select an item from the listBox the "Add artwork" button is activated. In the form with the addition of the work is also present 4 textbox .

How to make so that at a choice of an element in listbox1 form1 the information of this writer with all added works was output? In other words, how to link the bibliography with a specific author?

  • and where is the structure of the database? - Radzhab
  • Aha, and what ORM is used? - Dmitry
  • look at an example of binding hierarchical data in the answer - here - Stack
  • If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky ♦

1 answer 1

First, decide where you want to store your data. You can use the Code First approach for the Entity Framework. Create two classes:

 public class Artist //Π°Π²Ρ‚ΠΎΡ€(ΠΈΡΠΏΠΎΠ»Π½ΠΈΡ‚Π΅Π»ΡŒ) { public int ID {get;set;} //ΠΏΠ΅Ρ€Π²ΠΈΡ‡Π½Ρ‹ΠΉ ΠΊΠ»ΡŽΡ‡ public string FirstName {get;set} //ΠΈ Ρ‚.Π΄. public virtual ICollection<Track> Tracks {get;set;} public Artist() { Tracks = new List<Track>(); } } public class Track //Ρ‚Ρ€Π΅ΠΊ(ΠΏΡ€ΠΎΠΈΠ·Π²Π΅Π΄Π΅Π½ΠΈΠ΅) { public int ID {get;set;} //ΠΏΠ΅Ρ€Π²ΠΈΡ‡Π½Ρ‹ΠΉ ΠΊΠ»ΡŽΡ‡ public int ArtistId {get;set;} //внСшний ΠΊΠ»ΡŽΡ‡ public Artist Artist {get;set;} //свойство Π½Π°Π²ΠΈΠ³Π°Ρ†ΠΈΠΈ public string Name {get;set;} //ΠΈ Ρ‚.Π΄. } 

This code will create a one-to-many relationship between your entities. You can associate collections of authors and works by calling the System.Linq.Iqueryable<T>.Include<T,object>(Expression<Func<T,object>> path) where T : class on the entity instance. In the method itself, simply pass in a lambda expression that determines what to "bind".