Suggested Videos
Part 18 - Editing and updating data in gridview using objectdatasource control
Part 19 - Using optimistic concurrency when editing data in gridview
Part 20 - Keep gridview in edit mode when update fails due to data conflict
In this video we will discuss about using TemplateField with GridView control when editing data. Please watch part 20 of asp.net gridview tutorial before proceeding.
By default, when editing data, a textbox is rendered for all the bound columns. It would make more sense to use dropdownlist as the editing interface for "Gender". To achieve this we have to use a "TemplateField" instead of "BoundField" for "Gender" column. This can be very easily achieved using the deisgner or directly thru the HTML source.
Step 1: Click on "Edit Columns" link from GridView Tasks pane
Step 2: Click on "Gender" from "Selected Fields" section, and then click on "Convert this field into a Template Field" link and finally click OK.
Step 3: At this point, you should be back on gridview tasks pane. Click on "Edit Templates" link
Step 4: From "Display" dropdownlist, select "EditItemTemplate". Replace the "TextBox" control with a "DropDownList".
Step 5: Now click on "Edit Items" link from "DropDownList Tasks" pane. Add the 3 listem items (Please Select, Male, Female) to the dropdownlist and click OK. Finally click on "End Template Editing" from GridView Tasks pane.
Now run the application and click on EDIT. Notice that, "Gender" DropDownList always has "Please Select" as the selected item. This is incorrect. When the row is in "Edit" mode we want to have the correct "Gender" selected. To achieve this follow the instructions in Step 6 below.
Step 6: Now flip the webform to HTML source mode. Set "SelectedValue" property of dropdownlist as shown below. Steps 1 to 6 has converted, the gender bound field to template field. The template field HTML markup that is auto-generated is shown below.
Now run the application and click on EDIT. Notice that a dropdownlist control is now used as editing interface for gender column in the gridview control.
Part 18 - Editing and updating data in gridview using objectdatasource control
Part 19 - Using optimistic concurrency when editing data in gridview
Part 20 - Keep gridview in edit mode when update fails due to data conflict
In this video we will discuss about using TemplateField with GridView control when editing data. Please watch part 20 of asp.net gridview tutorial before proceeding.
By default, when editing data, a textbox is rendered for all the bound columns. It would make more sense to use dropdownlist as the editing interface for "Gender". To achieve this we have to use a "TemplateField" instead of "BoundField" for "Gender" column. This can be very easily achieved using the deisgner or directly thru the HTML source.
Step 1: Click on "Edit Columns" link from GridView Tasks pane
Step 2: Click on "Gender" from "Selected Fields" section, and then click on "Convert this field into a Template Field" link and finally click OK.
Step 3: At this point, you should be back on gridview tasks pane. Click on "Edit Templates" link
Step 4: From "Display" dropdownlist, select "EditItemTemplate". Replace the "TextBox" control with a "DropDownList".
Step 5: Now click on "Edit Items" link from "DropDownList Tasks" pane. Add the 3 listem items (Please Select, Male, Female) to the dropdownlist and click OK. Finally click on "End Template Editing" from GridView Tasks pane.
Now run the application and click on EDIT. Notice that, "Gender" DropDownList always has "Please Select" as the selected item. This is incorrect. When the row is in "Edit" mode we want to have the correct "Gender" selected. To achieve this follow the instructions in Step 6 below.
Step 6: Now flip the webform to HTML source mode. Set "SelectedValue" property of dropdownlist as shown below. Steps 1 to 6 has converted, the gender bound field to template field. The template field HTML markup that is auto-generated is shown below.
Now run the application and click on EDIT. Notice that a dropdownlist control is now used as editing interface for gender column in the gridview control.
hello sir . can you please tell me why i am not getting SelectedValue property in dropdownlist and are we can do this in code behind page. thanks
ReplyDeleteyou have to select "Edit Data Bindings" property of Dropdownlist.
Deletethen you will get that property.
Hi Venkat, I am Sanjay. This tutorial is very.......... nice. Your way of teaching is very nice. i want to ask one question.
ReplyDeleteHow can we update or delete multiple record into database using gridview, can u upload this video please.
heloo sir
ReplyDeleteI want automatically add new row in grid view with text box on tab key press so sir kindly suggest me...
hello sir,
ReplyDeleteafter setting Data Binding Property of Dropdownlist ...i am getting error which is
'DropDownList1' has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value
can you pls help me out..
thanks in advance
This is cus you have spaces in your gender values in the database table
Deleteyou have 'Female ' you must have 'Female'
Yes , it doesn't found , but write it as it and recompile you will never receive error
DeleteI have the same problem
ReplyDeletehello sir . can you please tell me why i am not getting SelectedValue property in dropdownlist and are we can do this in code behind page. thanks
ReplyDeleteIn DataBase, Change your Data Type of Gender Table to "varchar()".
ReplyDeleteIts work for me.