Click here to Skip to main content
5,785,816 members and growing! (18,903 online)
Email Password   helpLost your password?
Web Development » ASP.NET » Data License: The Code Project Open License (CPOL)

Drop down list as Itemtemplate within Gridview with Object Data Source Updating Database

By Ranjan.D

A grid view displaying records with Drop down list as one feild and updating database with Object data source, A problem occurs with Drop down's Selected value cant be passed as a parameter, so its solved here in this article by programatically adding update parameter in code behind
C# (C# 1.0, C# 2.0, C# 3.0, C#)

Posted: 3 Feb 2008
Updated: 3 Feb 2008
Views: 20,542
Bookmarked: 19 times
Note: This is an unedited reader contribution
Announcements
Loading...



Search    
Advanced Search
Sitemap
8 votes for this Article.
Popularity: 3.56 Rating: 3.94 out of 5
1 vote, 12.5%
1
1 vote, 12.5%
2
0 votes, 0.0%
3
1 vote, 12.5%
4
5 votes, 62.5%
5
Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report This Article
GridView.JPG

Introduction

Have you ever faced any problem in Grid view with Drop down list as Item Template and you try to update Grid view records using Object data source

From long time we used Data grid and its binding, also we saw how we can add drop down list as an Item template in it and it’s not new.

This article mainly deals with Display a drop down list inside a Grid view whose data source is binded and selected value is set based on ID. Updating grid row with object data source connecting business logic code.

I was just playing with Grid view and though of displaying a Drop down inside grid view with Grid view‘s data source is set to Object data source which connects to Business logic code. There’s nothing new in displaying Drop down in Grid view and its data source can also be set and selected value can be set inline.

But when using Object data source with say Grid view or Form view and trying to update the records which includes a drop down as one template column then comes the problem.

While updating using object data source you can have a set of Update Parameters with 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 Object data source’s Update Parameters while you update database though Object data source connecting your business logic code.

Simple parameters can be set with Parameter. Did you ever though of how we can send selected value of Drop down list as a parameter with object data source.

If you think of sending through Control Parameter then it’s not possible. We can use Control parameter with its Id can be set to Grid view and not the drop down inside Grid view.

The solution which one I found is simply adding an update parameter in code behind. Let me explain how I did all these things.

On Grid view’s OnRowCommand Event just added an update parameter for Object data source’s Update Parameter collection. Based on the grid view’s command name is Update


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 Grid view’s command event argument;s CommandArgument property.

2) Get the drop down list from the selected row through FindControl method on Gridview Row

3) Get the selected value of drop down and add parameter with selected value to Object datasource; s Update Parameters collection. OnrowUpdating Event of Gridview we can call Object datasource;s Update() method to invoke business logic Update method.

OnrowUpdating Event of Gridview we can call Object datasource;s Update() method to invoke business logic Update method.

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
        ObjectDataSource1.Update();
}

Points of Interest

Please don’t try to open 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 code project and run it one's. A project is worth than imagining how it is?

License

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

About the Author

Ranjan.D


"You can believe a Dog or a Cat but never a Human being"
Occupation: Web Developer
Location: India India

Other popular ASP.NET articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 1 of 1 (Total in Forum: 1) (Refresh)FirstPrevNext
GeneralVery good articlemembergirishdpatil1:48 15 Oct '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 3 Feb 2008
Editor:
Copyright 2008 by Ranjan.D
Everything else Copyright © CodeProject, 1999-2009
Web09 | Advertise on the Code Project