ASP.NET 4.0 and the Entity Framework 4 - Part 5 - Using the GridView and the EntityDataSource
page 5 of 7
by Vince Varallo
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 34457/ 69

Step 4: Create Template Fields

Let's say that your user didn't want to use a textbox to edit the Title.  Instead they want a drop down list to be displayed.  You can accomplish this by creating a Template field within the GridView control.  Stop your project and follow these steps to create a drop down list when editing the Title in the GridView control.

1.    Drag a second EntityDataSource control onto your web form.

2.    Click the smart tag and click on the Configure Data Source link.

3.    Select AdventureWorksEntities from the Named Connections list and click the Next button.

4.    Change the EntitySetName to Contacts. 

5.    Uncheck the Select All(Entity Value) checkbox and check the box next to the Title field.

6.    Click the Finish button.

7.    Click the Source View so you can see the markup.  You only want this control to select the distinct list of titles from the table and you want them to be ordered alphabetically.  You should see the following code in the markup view.

<asp:EntityDataSource ID="EntityDataSource2" runat="server" 
    DefaultContainerName="AdventureWorksEntities" EnableFlattening="False" 
    EntitySetName="Contacts" Select="it.[Title]">


8.    Change the Select attribute to

   Select="DISTINCT it.[Title]"

9.    Add the following attribute to order the records alphabetically.


10. The markup should look as follows:

<asp:EntityDataSource ID="EntityDataSource2" runat="server" 
    DefaultContainerName="AdventureWorksEntities" EnableFlattening="False" 
    EntitySetName="Contacts" Select="DISTINCT it.[Title]" OrderBy="it.[Title]">

11. Click back to Design view.

12. Click the smart tag on the GridView control and select Edit Columns… from the pop-up menu.

13. Click on the Title field in the Selected Fields list.

14. Click on the link above the OK button called "Convert this field into a TemplateField".

15. Now click the OK button.

16. Click the smart tag on the GridView control again and select Edit Templates from the pop-up menu.

17. The item template displays the controls displayed in the grid view when simply looking at the grid.  In this example the item template for the Title field is a label control.  The value of the title field will be displayed in the lable.

18. Click on the EditItemTemplate from the list.  This will allow you to change the control from a textbox to a drop down list.

19. Click on the textbox in the EditItemTemplate and delete it.

20. Drag a DropDownList control into the EditItemTemplate.

21. Click the smart tag next to the DropDownList control and select the Choose DataSource link.

22. Choose EntityDataSource2 from the Select a Data Source list.

23. Click the Refresh Schema link.  The Title field should be displayed in the drop down lists on the form.  Click the OK button.

24. This will populate the drop down list with the distinct list of values in the Title field from the table.  Now you have to configure the control to display the correct selected value based on the record you are editing.

25. Click the smart tag next to the DropDownList again.  Select Edit Data Bindings… from the pop-up menu.

26. The DataBinding form will appear.  In the Code Expression text box enter the text "Bind("Title")".  This will select the value in the drop down list based on the value of the title field of the selected record.

27. Click the OK button.

28. Run the project again and click on the Edit link of the first record.  You should see the DropDownList and the correct value selected.

View Entire Article

User Comments

Title: vb nvvbnvb   
Name: vbnvbn
Date: 2012-06-06 10:09:02 PM
Title: Cant see images   
Name: jrk
Date: 2012-05-25 9:27:32 PM
The images in this article show as big red X
Title: great done   
Name: vishwajeet singh
Date: 2010-09-30 6:44:04 AM
its realy great thing what happend through .net 4.0

Product Spotlight
Product Spotlight 

Community Advice: ASP | SQL | XML | Regular Expressions | Windows

©Copyright 1998-2024  |  Page Processed at 2024-06-23 8:31:01 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search