Windows Develop Bookmark and Share   
 index > Windows Forms General > Basic question about loading DataGridView
 

Basic question about loading DataGridView

Hello,

I hope you'll excuse my ignorance, but I've got what I think is a very basic DataGridView question. I'm using the Entity Framework. Let's say I've got an Author and Book table. I want to show the list of books with the author in a DGV. Should I manually create the columns and programmatically load the rows? Or do I need to create a new class that contains the fields I want to display and use that as the data source?Or what is the preferred method?

I have a feeling I've been taking the long road for a while when it comes to loading data into a DataGridView. Thanks for any help, I really appreciate it.

Thanks,
Nick
nickfinity  Friday, September 11, 2009 8:57 PM
You can do something like this:

''Let's assume your DataContext is called "db"

datagridView1.DataSource = db.Authors
.Join(
db.Books,
a => a.AuthorID
b => b.AuthorID,
(a, ab) => new
{
//List what fields you want
BookName = b.BookTitle
}).Where(a => a.AuthorName == "John");
John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
  • Marked As Answer bynickfinity Monday, September 14, 2009 5:55 PM
  •  
JohnGrove  Friday, September 11, 2009 9:24 PM
dataGridView.DataSource = Books.Where(i => i.AuthorName == "John");
John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
JohnGrove  Friday, September 11, 2009 9:09 PM
Is Book joined to Author? You may have to do a join in LINQ first.
John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
JohnGrove  Friday, September 11, 2009 9:11 PM
Thanks for the help John. I want to show the author name and book name, so I do need to do a join. If I do that will everything show up? I'll give it a try.

Thanks,
Nick
nickfinity  Friday, September 11, 2009 9:13 PM
What are the fields? Can youlist them please. Both entities..
John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
JohnGrove  Friday, September 11, 2009 9:15 PM
You can do something like this:

''Let's assume your DataContext is called "db"

datagridView1.DataSource = db.Authors
.Join(
db.Books,
a => a.AuthorID
b => b.AuthorID,
(a, ab) => new
{
//List what fields you want
BookName = b.BookTitle
}).Where(a => a.AuthorName == "John");
John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
  • Marked As Answer bynickfinity Monday, September 14, 2009 5:55 PM
  •  
JohnGrove  Friday, September 11, 2009 9:24 PM
Hi nickfinity,

In addition to JohnGrove's reply, let me answer some questions in your post.

>Should I manually create the columns and programmatically load the rows?

If you use Databinding for DataGridView as JohnGrove suggested, you don't need to manually create columns and load rows. What you need to do is just set the DataSource property of DataGridView.

>If I do that will everything show up?

Yes, the "author name" and "book name" will be shown on the DataGridView.

If you have anything unclear, please feel free to tell me.

Sincerely,
Kira Qian
Send us any feedback you have about the help from MSFT at fbmsdn@microsoft.com.
Please remember to mark the replies as answers if they help and unmark them if they provide no help. Welcome to the All-In-One Code Framework!
Kira Qian  Monday, September 14, 2009 3:24 AM
Thanks for your help. I haven't got back to this project yet today, but this is what I was looking for. Thanks again.
nickfinity  Monday, September 14, 2009 5:56 PM

You can use google to search for other answers

Custom Search

More Threads

• custom UserControl
• Form Will Not Minimize Sometimes
• Child form's Close event not fired to listening parent form
• Form Inheritance and the Anchor Property
• Editing Data in a Form Using SQL
• How do I implement copy/paste listview which has multiple colum?
• Icons
• Which control to use if..
• Description of my function and his parameters
• Custom Control that handles Format/Parse internally