Solved

ASP.net 3.5 GridView Displaying DataTextField in ItemTemplate Column

Posted on 2010-08-16
7
706 Views
Last Modified: 2012-05-10
I give up.  I'm trying to create a GridView that displays a value in a related table in the Item Template field.  Here's what I have.

I created a test WebSite with the following tables

Customer = CustomerID, CustomerName, CustomerTypeCode
CustomerType = CustomerTypeCode, CustomerType

I defined the relationship between the tables in SQL Server CustomerTypeCode = CustomerTypeCode

Then I created the test web site.  I'm using Linq to Sql so I created a DBML file.  Dragged the tables to the DBML.   THe links appear as entered.  

THen I added an asp web form and dragged a Gridview to the form.    Next I set the Datasource as a Linq to SQL datasource based on the Customer table.  I changed the CustomerTypeCode column to a Template Field

THen I went to the source code and changed the eval("CustomerTypeCode") to eval("CustomerType") in the Item Template.  

When I display in Browser, I get the following message.


DataBinding: 'Customer' does not contain a property with the name 'CustomerType'.

What am I doing wrong?
0
Comment
Question by:deepPowdah
7 Comments
 
LVL 41

Accepted Solution

by:
guru_sami earned 168 total points
ID: 33450366
I don't think it will work that way. Since you are binding your GV to Customers table you won't get CustomerType.
You will have to write a separate join query that will return the Customer Columns + CustomerType in one resultset and then bind that to your GV.
0
 

Author Comment

by:deepPowdah
ID: 33450675
I have tried that but when you add the Customer Type table the recordset is no longer updateable.  I attempted to add the CUstomerType table using a view.  So when you try to edit a customer record, say change the customer name, you get an error message that it cannot be updated.  
0
 

Author Comment

by:deepPowdah
ID: 33450685
Using the Linq to SQL you must choose 1 item as your data source.  So it must be either the CustomerTable by itself, or the CustomerTable and CustomerType Table in a View.  How else would you do this/
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 16

Assisted Solution

by:Kamal Khaleefa
Kamal Khaleefa earned 166 total points
ID: 33452629
write a query like this
select
CustomerID, CustomerName, CustomerTypeCode,
CustomerTypeCode, CustomerType
from
Customer inner join
CustomerType on
Customer.CustomerTypeCode=CustomerType .CustomerTypeCode
0
 

Author Comment

by:deepPowdah
ID: 33453424
I used a left outer join to do this in a view.  I believe you can only use a table or view as a source for a Linq to SQL datasource.  The left outer join ensures that all customers are displayed even if they do not have a customer type.

SELECT     dbo.Customer.*, dbo.CustomerType_Lookup.CustomerType
FROM         dbo.Customer LEFT OUTER JOIN
                      dbo.CustomerType_Lookup ON dbo.Customer.CustomerTypeCode = dbo.CustomerType_Lookup.CustomerTypeCode

When I attempt to edit a record based on this view, I get the following error message.

Could not find a row that matches the given keys in the original values stored in ViewState.  Ensure that the 'keys' dictionary contains unique key values that correspond to a row returned from the previous Select operation
0
 
LVL 2

Assisted Solution

by:ahmadmaredia
ahmadmaredia earned 166 total points
ID: 33453655
Assuming you are using SQL Server 2005 or above, try the following query... This should generate a unique key for each row of your resultset for ASP.Net to work properly.  In the first line of the query, replace CustomerId with the column name of the PK of your Customer table.
SELECT     ROW_NUMBER() OVER (ORDER BY CustomerId ASC) AS ROWID,

dbo.Customer.*, dbo.CustomerType_Lookup.CustomerType

FROM         dbo.Customer LEFT OUTER JOIN

                      dbo.CustomerType_Lookup ON dbo.Customer.CustomerTypeCode = dbo.CustomerType_Lookup.CustomerTypeCode

Open in new window

0
 

Author Comment

by:deepPowdah
ID: 33470571
Thanks for all your input.  I have figured out how to do this using Linq to SQL.  When you create a data content page (dbml) by dragging tables from the Server View, the relationships you created in SQL Server are brought across too.  Then when you create the Linq DataSource for the GV, there is what appears to be an extra field for the name of each related table.   Then you create a template field for the lookup field, e.g. CustomerTypeCode. You can add a drop down list to enter and update the CustomerTypeCode.  However in the Item Template, you can display the CustomerType, not the CustomerType using the following code.

                    <ItemTemplate>
                        <asp:Label runat="server" Text='<%#Eval("CustomerType_Lookup.CustomerType") %>'></asp:Label>
                    </ItemTemplate>

Linq to SQL has its moments and this capability is one of them.  I wish someone would write a book about how to use Linq to SQL.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now