How to Bind Dropdownlist Inside Gridview Edit Template Asp.net C#

7

Overview:  This article explains how to bind drop down list control which is placed inside Asp.net Gridview control under Edit Template. .i.e Gridview edit template dropdownlist bind. Also using DataRowView we able to get and set gridview edit template drop-down list value.

Here will show an example on how to bind data to Asp.net Drop down list control of Gridview control placed under Edit Template.

Steps to bind drop-down list in Gridview while Editing.

1) Add Html markup i.e (Gridview) 2) On Page load bind Gridview control. 3) On RowDataBound bind Dropdownlist.


Html markup:

Here first we add a Gridview control, by just drag and drop on our web page. Now will use item template to display data .i.e (we are are going to show employee name, gender, location, and department).

Now we want to edit the department for selected employee. So now we added a drop-down list under edit template, which will display only on edit mode. Our final HTML looks like as written below.


Page_load Bind gridview control:

This is a simple task, we made a common function gvBind() which binds our Gridview control with data.

On page load will call this function, and now whenever the page gets load, function gvBind() gets call and data gets display. Our final code looks as shown below.


On RowDataBound: Bind drop-down list

Now we are at the main part of this article .i.e we are going to bind dropdown list in Gridview which we placed under edit template.

Just look at HTML markup you will see ddl_department named drop-down list added. On RowDataBound by using FindControl will get the dropdown list and then bind it with our DataTable.

Bonus : Now in edit mode we will set gridview dropdown list selected value as it displays before edit mode.

For this, we are using DataRowView and able to set dropdown list default selected value. The final code looks like as written below.

  Yeah, we are done now 🙂 Hope you enjoyed this tutorial. If you have any recommendations, please let us know what you think in the comment section below! See you again next time! 


Get your Printable Copy

7 COMMENTS

  1. I have a simple form called registration.aspx for users. Its works fine and save to database .

    regID, userName,email ,countryID (interger,FK), photo . How can I make a edit profil page for the users where they can edit the information

    • That’s easy, you just need to create a new page as myProfile.aspx and add textboxes and FileUpload control and button.
      On button click get all the values and then fire your update query. In your case SQL query would be like
      update TableName set userName==’val2′,email=’val3′ ,countryID =’val4′, photo=’val5′ where regID=’uniqueId’ .

      For uploading photo, you can check at Upload Photo in Asp.net C#

      Note: While login save the unique user id into Session variable, so you can use it in your update query

LEAVE A REPLY