Edit Update GridView DropDownList SelectedValue

Updated on 16 Nov 2010,
Published on 07 Aug 2008

You can edit and update the SelectedValue property of DropDownList inside the GridView control using the RowUpdating event of the GridView control. In this tutorial for learning the Edit and update action on GridView DropDownList SelectedValue we will again consider the SQL Database tables i.e. tbl_orders and tbl_order_status that we discussed in ASP.Net GridView DropDownList column. Here we have extended the last example of DropDownList placed inside the GridView ItemTemplate of TemplateField Column. Only one extra function has been introduced in this tutorial to edit and update the SelectedValue of DropDownList using RowUpdating event. Another two GridView events such as RowEditing and RowCancelingEdit are also used here to perform the action for changing the modes of GridView Control to edit mode and back to read-only display mode.

You can extend the C# code behind of the sample code provided in the previous article: GridView DropDownList SelectedValue in ASP.Net by adding the following code to handle the edit and update events.

C# Code to Edit and Update the GridView DropDownList SelectedValue

[code:c#]protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
    GridView1.EditIndex = e.NewEditIndex;
    bindGridView();
}
 
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
    GridView1.EditIndex = -1;
    bindGridView();
}
 
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
    // string variable to store the connection string
    // defined in appsettings section of web.config file.
    string connStr = ConfigurationManager.ConnectionStrings[ "ConStr" ].ConnectionString;


    // object created for SqlConnection Class.
    SqlConnection mySQLconnection = new SqlConnection(connStr);


    // if condition that can be used to check the sql connection
    // whether it is already open or not.
    if (mySQLconnection.State == ConnectionState.Closed)
    {
        mySQLconnection.Open();
    }


    // SQL Command object to pass the SQL query for updating the data of tbl_orders table.
    SqlCommand mySqlCommand = new SqlCommand("update tbl_orders set order_status_id=@order_status_id where order_id=@order_id", mySQLconnection);
 
    DropDownList drdList;
 
    drdList = (DropDownList)(GridView1.Rows[ e.RowIndex ].Cells[1].FindControl( "DropDownList1" ));


    // SQL command input parameter to set the new value for order_status_id
    mySqlCommand.Parameters.Add( "@order_status_id", SqlDbType.Int).Value = drdList.SelectedValue;


    // SQL command input parameter as order_id whose corresponding value for order_status_id is to be set
    mySqlCommand.Parameters.Add( "@order_id", SqlDbType.Int).Value = Convert.ToInt32( GridView1.DataKeys[ e.RowIndex ]["order_id"]);


    // Execute the SQL query to update the tbl_orders
    mySqlCommand.ExecuteNonQuery();


    // if condition that can be used to check the sql connection
    // if it is open then close it.
    if (mySQLconnection.State == ConnectionState.Open)
    {
        mySQLconnection.Close();
    }

    GridView1.EditIndex = -1;
    bindGridView();
}[/code]

Output:

Get the output of above discussed code from the following link:

GridView Edit Update Dropdownlist Values

Continue reading to the next tutorial: GridView DropDownList Update SelectedValue All At Once to learn how to update the SelectedValue of all the Dropdownlist controls placed inside the GridView control with a single click.

1 Responses to "Edit Update GridView DropDownList SelectedValue"
Suresh P
gggooooooooooooddd
Leave a Comment
* required
* required
* will not be published
* optional
* hint: http://www.example.com
  • Subscribe via Email