Wrox Programmer Forums
|
BOOK: Beginning ASP.NET 4.5 : in C# and VB
This is the forum to discuss the Wrox book Beginning ASP.NET 4.5: in C# and VB by Imar Spaanjaars; ISBN: 978-1-118-31180-6
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning ASP.NET 4.5 : in C# and VB section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 9th, 2015, 04:11 PM
Authorized User
 
Join Date: Aug 2015
Posts: 19
Thanks: 6
Thanked 0 Times in 0 Posts
Default EF/Gridview/Related tables

There must be a way to modify the code on P 503 to include GenreName instead of GenreID in the Gridview. Please show me how that might work. Thank you.
 
Old October 10th, 2015, 04:02 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi there,

There are a couple of different ways to do it. One solution is to use Include (see page 505) to include the Gene and then build a new anonymous object (see page 512) to select the properties from Review you need and also include the Genre.Name, like this:

Code:
var authorizedReviews = from review in myEntities.Reviews.Include("Genre")
                        where review.Authorized == true
                        orderby review.CreateDateTime descending
                        select new
                        {
                          review.Id,
                          review.Title,
                          review.Summary,
                          review.Body,
                          review.GenreId,
                          review.Genre.Name,
                          review.Authorized,
                          review.CreateDateTime,
                          review.UpdateDateTime
                        };

GridView1.DataSource = authorizedReviews;
GridView1.DataBind();
This selects all properties from the Review but also includes review.Genre.Name which is then rendered in the GridView in the Name column. If you want to rename it, you could do something like this:

GenreName = review.Genre.Name,

Instead of relying on automatically created columns you can also convert the GridView to templates and then bind the genre to Genre.Name instead of GenreId.

Hope this helps,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
The Following User Says Thank You to Imar For This Useful Post:
Grodnik (October 17th, 2015)
 
Old October 17th, 2015, 02:51 PM
Authorized User
 
Join Date: Aug 2015
Posts: 19
Thanks: 6
Thanked 0 Times in 0 Posts
Default

Thank you - very helpful. I just used BoundFields in the Gridview instead of templates. I created an anonymous type that included the tableID of the main table, but I didn't include tableID as a gridview column. Nonetheless, when I clicked on the row command button, the subroutine managed to convert the index to the right tableID. I'm not exactly sure how that works. Have you got a page reference?
 
Old October 22nd, 2015, 05:22 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

You probably have a DataKeyNames attribute set that ASP.NET uses to keep track of the primary key(s) for each row,

Hope this helps,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!





Similar Threads
Thread Thread Starter Forum Replies Last Post
EF/Gridview/Related tables Grodnik BOOK: Beginning ASP.NET 4.5 : in C# and VB 0 October 9th, 2015 02:21 PM
Related tables Dale Access 3 March 19th, 2012 05:23 PM
Questions related to ADO.NET EF and one unrelated Rushino ASP.NET 4 General Discussion 6 July 29th, 2010 08:41 AM
Synchronising Two Sets of Related Tables Odeh Naber Access 11 May 14th, 2007 06:58 AM
Insert record Into 2 related tables at once kalchev ASP.NET 2.0 Basics 2 May 9th, 2006 05:10 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.