
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:
- Control Parameter
- Cookie Parameter
- Form Parameter
- Parameter
- Profile Parameter
- Query String Parameter
- 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());
}
}
- 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.
- Get the drop down list from the selected row through the
FindControl method on the GridView row.
- 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?