单击GridView更新链接时无法更新数据库 - 恢复为旧值

时间:2015-01-20 21:14:01

标签: c# asp.net gridview

我尝试允许我的用户编辑GridView中的某些列。我当然看了很多代码,并尝试了很多代码无济于事。使用调试我的值看起来很好,我只是没有更新我的数据库表。对我遗漏的任何帮助都会有所帮助。再次感谢。

//Course Name, Course Rating or the Course Slope
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{

    GridView1.EditIndex = e.NewEditIndex;
    GridView1.DataBind();

}//end method GridView1_RowEditing

protected void GridView1_RowUpdating(object sender,GridViewUpdateEventArgs e)
{
    GridViewRow currentRow = GridView1.Rows[GridView1.EditIndex];

    currentCourseName = ((TextBox)currentRow.Cells[3].Controls[0]).Text;

    //Get the updated value from GridView
    string currentRating = ((TextBox)currentRow.Cells[4].Controls[0]).Text;
    string currentSlope = ((TextBox)currentRow.Cells[5].Controls[0]).Text;
    string currentCourseID = GridView1.Rows[GridView1.EditIndex].Cells[2].Text;


    theCourseID = Int32.Parse(currentCourseID);
    newRating = float.Parse(currentRating);
    newSlope = Int32.Parse(currentSlope);

    using (GolfDatabaseEntities10 myEntities = new GolfDatabaseEntities10())
    {

        Course obj = myEntities.Courses.First(courseSelected => courseSelected.Course_ID == theCourseID);

        //Should set the values in the table Course
        obj.Course_Rating = newRating;
        obj.Course_Slope = newSlope;
        obj.Course_Name = currentCourseName;

        //Save all changes
        myEntities.SaveChanges();

        //leave edit mode
        GridView1.EditIndex = -1;

        if(!IsPostBack)
            GridView1.DataBind();

    }//end using

}//end method GridView1_RowUpdating

0 个答案:

没有答案