Click here to Skip to main content
Click here to Skip to main content
Go to top

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

, 3 Feb 2008
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

GridView.JPG

Introduction

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)
{
        ObjectDataSource1.Update();
}

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?

License

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

Share

About the Author

Ranjan.D
Web Developer
United States United States
Profile
 
Around 9 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
 
Others:
 
TTD, BDD
 
Education
 
B.E (Computer Science)
 
CodeProject Contest So Far:
 
1. Windows Azure Developer Contest - HealthReunion - A Windows Azure based healthcare product , link - http://www.codeproject.com/Articles/582535/HealthReunion-A-Windows-Azure-based-healthcare-pro
 
2. DnB Developer Contest - DNB Business Lookup and Analytics , link - http://www.codeproject.com/Articles/618344/DNB-Business-Lookup-and-Analytics
 
3. Intel Ultrabook Contest - Journey from development, code signing to publishing my App to Intel AppUp , link - http://www.codeproject.com/Articles/517482/Journey-from-development-code-signing-to-publishin
 
4. Intel App Innovation Contest 2013 - eHealthCare - http://www.codeproject.com/Articles/635815/eHealthCare
 
5. Grand Prize Winner of CodeProject HTML5 &CSS3 Article Content 2014

Comments and Discussions

 
GeneralMy vote of 5 PinprofessionalMohamedKamalPharm15-Jun-13 21:05 
GeneralMy vote of 5 PinmemberP.Salini4-Jun-12 18:27 
QuestionThanks for this code PinmemberSubaMohan7-Mar-12 21:55 
QuestionGreat article...,thanks !!! PinmemberSanjay_00727-Nov-11 22:53 
QuestionUsing SQL Server database instead of Access database ? Pinmemberkibock20-Oct-11 3:50 
AnswerRe: Using SQL Server database instead of Access database ? PinmemberVictoria Rowe10-Dec-11 15:51 
QuestionCan I use for SqlDataSource? Trouble understanding the code PinmemberVictoria Rowe12-Sep-11 20:51 
GeneralWhy does my code not work Pinmemberhanzieman23-Jan-11 8:25 
GeneralThis is good article, please look at a new problem! Pinmemberthd_tq12326-Feb-09 22:51 
GeneralThanks for your sharing Pinmembernicholas_pei26-Feb-09 16:36 
Generalgreat, thanks PinmemberMember 116050210-Jan-09 10:41 
GeneralVery good article Pinmembergirishdpatil15-Oct-08 0:48 
This article is very good those who are follwing a 3-tire archetecture.

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 | Mobile
Web02 | 2.8.140916.1 | Last Updated 3 Feb 2008
Article Copyright 2008 by Ranjan.D
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid