Click here to Skip to main content
11,629,371 members (66,579 online)
Click here to Skip to main content

DropDownList as ItemTemplate within a GridView with an ObjectDataSource updating a database

, 3 Feb 2008 CPOL 97.5K 3.2K 46
Rate this:
Please Sign up or sign in to vote.
A GridView displaying records with a DropDownList as a field and updating a database with an ObjectDataSource. A problem occurs if the dropdown's selected value can not be passed as a parameter. The issue is solved here in this article by programatically adding an update parameter in the code-behind



Have you ever faced any problem in GridViews with a DropDownList as an ItemTemplate when you try to update the GridView records using an ObjectDataSource.

This article mainly deals with displaying a DropDownList inside a GridView whose data source is bound and the selected value is set based on the ID, and updating a grid row with ObjectDataSource connecting business logic code.

I was just playing with the GridView and thought of displaying a drop down inside a GridView with the GridView's data source set to an ObjectDataSource which connects to the business logic code. There’s nothing new in displaying a drop down in a GridView, and its data source can also be set and the selected value can be set inline.

But when using an ObjectDataSource with say a Grid view or Form view and trying to update the records which include a drop down as a template column, then comes a problem.

While updating using an ObjectDataSource, you can have a set of update parameters, where each parameter may be:

  1. Control Parameter
  2. Cookie Parameter
  3. Form Parameter
  4. Parameter
  5. Profile Parameter
  6. Query String Parameter
  7. Session Parameter

You can set any update parameter in an ObjectDataSource’s UpdateParameters while you update database though the ObjectDataSource connecting your business logic code.

Simple parameters can be set with Parameter. Have you ever thought of how we can send the selected value of a drop down list as a parameter with an ObjectDataSource? If you think of sending through the Control Parameter, then it’s not possible. We can use Control parameter with its ID set to the GridView and not the drop down inside the GridView.

The solution which I found is to simply add an update parameter in the code-behind. Let me explain how I did it.

On the GridView’s OnRowCommand event, just add an update parameter for the ObjectDataSource’s UpdateParameter collection.

Using the Code

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    if (e.CommandName.Equals("Update"))
        int state = 0;
        int index = int.Parse(e.CommandArgument.ToString());
        GridViewRow row = GridView1.Rows[index];
        DropDownList lstState = (DropDownList)row.FindControl("StateID");
        state = int.Parse(lstState.SelectedValue.ToString());
        ObjectDataSource1.UpdateParameters.Add("StateID", state.ToString());
  1. Select a GridView row; based on the GridView selection, we will get the row index based on the GridView’s command event argument's CommandArgument property.
  2. Get the drop down list from the selected row through the FindControl method on the GridView row.
  3. Get the selected value of the drop down and add the parameter with the selected value to the ObjectDataSource's UpdateParameters collection. In the OnrowUpdating event of the GridView, we can call the ObjectDataSource's Update() method to invoke the business logic Update method.
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)

Points of Interest

Please don’t try to open the sample website by just double clicking on Solution Explorer. Open VS 2005, File -> Open -> Website, and proceed. You will come to know more when you download the sample project and run it. A project is worth than imagining, is it not?


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Web Developer
United States United States

Around 10 years of professional software development experience in analysis, design, development, testing and implementation of enterprise web applications for healthcare domain with good exposure to object-oriented design, software architectures, design patterns, test-driven development and agile practices.

In Brief

Analyse and create High Level , Detailed Design documents.
Use UML Modelling and create Use Cases , Class Diagram , Component Model , Deployment Diagram, Sequence Diagram in HLD.

Area of Working : Dedicated to Microsoft .NET Technologies
Experience with : C# , J2EE , J2ME, Windows Phone 8, Windows Store App
Proficient in: C# , XML , XHTML, XML, HTML5, Javascript, Jquery, CSS, SQL, LINQ, EF

Software Development

Database: Microsoft SQL Server, FoxPro
Development Frameworks: Microsoft .NET 1.1, 2.0, 3.5, 4.5
UI: Windows Forms, Windows Presentation Foundation, ASP.NET Web Forms and ASP.NET MVC3, MVC4
Coding: WinForm , Web Development, Windows Phone, WinRT Programming, WCF, WebAPI

Healthcare Domain Experience

CCD, CCR, QRDA, HIE, HL7 V3, Healthcare Interoperability


B.E (Computer Science)

CodeProject Contest So Far:

1. Windows Azure Developer Contest - HealthReunion - A Windows Azure based healthcare product , link -

2. DnB Developer Contest - DNB Business Lookup and Analytics , link -

3. Intel Ultrabook Contest - Journey from development, code signing to publishing my App to Intel AppUp , link -

4. Intel App Innovation Contest 2013 - eHealthCare

5. Grand Prize Winner of CodeProject HTML5 &CSS3 Article Contest 2014

6. Grand Prize Winner of CodeProject Android Article Contest 2014

7. Grand Prize Winner of IOT on Azure Contest 2015

You may also be interested in...

Comments and Discussions

QuestionNot working with GUIDs Pin
lufonius10-Oct-14 1:35
memberlufonius10-Oct-14 1:35 
GeneralMy vote of 5 Pin
MohamedKamalPharm15-Jun-13 21:05
professionalMohamedKamalPharm15-Jun-13 21:05 
GeneralMy vote of 5 Pin
P.Salini4-Jun-12 18:27
memberP.Salini4-Jun-12 18:27 
QuestionThanks for this code Pin
SubaMohan7-Mar-12 21:55
memberSubaMohan7-Mar-12 21:55 
QuestionGreat article...,thanks !!! Pin
Sanjay_00727-Nov-11 22:53
memberSanjay_00727-Nov-11 22:53 
QuestionUsing SQL Server database instead of Access database ? Pin
kibock20-Oct-11 3:50
memberkibock20-Oct-11 3:50 
Hi, Thank you very much for article. It's very unique and useful.

This article works 100% as it is. It uses DataFile = "~/App_Data/db1.mdb";
But I would like to use SQL Server databse instead of Access database.
How to modify the code in "" or "" in the App_Code so that the SQL Server database is fired?
Have someone already done the conversion?

Thank you very much in advance!
AnswerRe: Using SQL Server database instead of Access database ? Pin
Victoria Rowe10-Dec-11 15:51
memberVictoria Rowe10-Dec-11 15:51 
QuestionCan I use for SqlDataSource? Trouble understanding the code Pin
Victoria Rowe12-Sep-11 20:51
memberVictoria Rowe12-Sep-11 20:51 
GeneralWhy does my code not work Pin
hanzieman23-Jan-11 8:25
memberhanzieman23-Jan-11 8:25 
GeneralThis is good article, please look at a new problem! Pin
thd_tq12326-Feb-09 22:51
memberthd_tq12326-Feb-09 22:51 
GeneralThanks for your sharing Pin
nicholas_pei26-Feb-09 16:36
membernicholas_pei26-Feb-09 16:36 
Generalgreat, thanks Pin
Member 116050210-Jan-09 10:41
memberMember 116050210-Jan-09 10:41 
GeneralVery good article Pin
girishdpatil15-Oct-08 0:48
membergirishdpatil15-Oct-08 0:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150723.1 | Last Updated 3 Feb 2008
Article Copyright 2008 by Ranjan.D
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid