简体   繁体   中英

How can I bring from a database many values separated by “commas” to a cell of a datagridview in c#?

In my database, there are a Book table, a Category table and an Author table. A book has a Category and many Authors. There is an intermediate table between book and author. What I want to do is to bring a list of books to a datagridview.

In case of categories: should I set in the book class, a property: "category id" (int type) or a property: "category" (Category type) or just the "category_name" (String type)?

And in case of the authors: Should I set in the Book class, a property with a list of "authors" (Author type) or a list of "id authors" (int type) or a list of "authors name" (String type)

How should I make the query for bring the books from the database? In case of the authors: Should I make a query to bring all the books and then a query for each book to fill the authors of each?

I'm setting the list of books in the datagridview.datasource.

How do I show the list of books in a datagridview if the book class has included a category property and a property with a list of authors? (In a cell should be all book authors separated by commas)

Based on what I have done in one of my application, I suggest as under:

  1. Instead of placing everything in single DataGridView, use three different DataGridViews:
    • one for showing list of Books
    • one for showing list of Categories assigned to a book; being child of Books
    • one for showing list of Authors who wrote a book; being child of Books
  2. Create a DataSet containing three tables: Book, BookCategory and BookAuthor. Create two parent/child relationship: one Book and BookCategory, another Book and BookAuthor.
  3. Create three BindingSources one for each table: Book, BookCategory and BookAuthors. Bind each DataGridView to respective BindingSource objects.

Is it too late to start using Entity Framework ?

Entity Framework will do everything for you. You will not care about SQL low level. Of course, you still need to understand the structure and how things are related... but it makes your life much easier.

https://msdn.microsoft.com/en-us/data/jj193542(v=vs.113).aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM