locked
Gridview not updating when SQL has select statement in code behind RRS feed

  • Question

  • User1206964799 posted

    I have a gridview with a sql datasource. When the select statement for the datasource is in the sqldatasource control then everything works fine and i can view the data and update it through the gridview. When I take away the selecet statement and insetad use a sqldatasource.SelectCommand in my code behind then I can only view the data in the gridview but cannot edit. When I try to edit/update data the data being edited goes back to original and the gridview stays in edit mode. After hitting cancel on the gridview then the old data still shows. Any ideas about this? "Optimistic Concurncy" is also switched off so that is not the problem.

    Sunday, April 10, 2011 12:02 AM

Answers

  • User1508394307 posted

    You need to handle the RowUpdating event of GridView to assign the values to parameters manually:

    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
    string id = GridView1.DataKeys[e.RowIndex].Value.ToString();
    string txt = ((TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text;
    SqlDataSource1.UpdateParameters["id"].DefaultValue = id;
    SqlDataSource1.UpdateParameters["txt"].DefaultValue = txt;
    SqlDataSource1.Update();
    SqlDataSource1.SelectCommand = "...";
    }
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, April 10, 2011 5:25 AM

All replies

  • User1508394307 posted

    You need to handle the RowUpdating event of GridView to assign the values to parameters manually:

    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
    string id = GridView1.DataKeys[e.RowIndex].Value.ToString();
    string txt = ((TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text;
    SqlDataSource1.UpdateParameters["id"].DefaultValue = id;
    SqlDataSource1.UpdateParameters["txt"].DefaultValue = txt;
    SqlDataSource1.Update();
    SqlDataSource1.SelectCommand = "...";
    }
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, April 10, 2011 5:25 AM
  • User3866881 posted

    How did you code?

    Maybe you should offer us some parts of your codes.

    Thx:)

    PS:This is a detailed sample for you to refer about Editing/Inserting/Updating with SqlDataSource:http://csharpdotnetfreak.blogspot.com/2009/05/gridview-sqldatasource-insert-edit.html

    Tuesday, April 12, 2011 9:46 PM