![]() |
Web Development »
ASP.NET »
Data
License: The Code Project Open License (CPOL)
Drop down list as Itemtemplate within Gridview with Object Data Source Updating DatabaseBy Ranjan.DA 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)
|
||||||||||
|
Advanced Search |
|
|
|
||||||||||||||||
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
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.
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.
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
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(); }
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?
| You must Sign In to use this message board. | |||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 3 Feb 2008 Editor: |
Copyright 2008 by Ranjan.D Everything else Copyright © CodeProject, 1999-2009 Web15 | Advertise on the Code Project |