Have you ever faced any problem in
GridViews with a
DropDownList as an
ItemTemplate when you try to update the
GridView records using an
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:
- Control Parameter
- Cookie Parameter
- Form Parameter
- Profile Parameter
- Query String Parameter
- Session Parameter
You can set any update parameter in an
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
The solution which I found is to simply add an update parameter in the code-behind. Let me explain how I did it.
OnRowCommand event, just add an update parameter for the
Using the Code
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
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());
- Select a
GridView row; based on the
GridView selection, we will get the row index based on the
GridView’s command event argument's
- Get the drop down list from the selected row through the
FindControl method on the
- Get the selected value of the drop down and add the parameter with the selected value to the
UpdateParameters collection. In the
OnrowUpdating event of the
GridView, we can call the
Update() method to invoke the business logic
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?
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.
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
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 - 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