Hi and TIA:
Have looked at a lot of code and tried many of them to no avail.
I am wanting to allow my users to Edit/Update certain columns of my GridView. Using debug, my variable values look good, but SaveChanges() just doesn't seem to do the job. No errors and I do return from edit mode, but the database table remains the same and the GridView textbox reverts back to old value. Any help would be appreciated.
Code:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GridView1.DataBind();
}
}
Code:
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
GridView1.DataBind();
}//end method GridView1_RowEditing
and finally,
Code:
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