protected void GetTodoList() { string SortString = Session["SortColumn"].ToString() + " " + Session["SortDirection"].ToString(); //Connect to EF using (TodoConnection db = new TodoConnection()) { //Query the Todos table using EF and LINQ var TodoItems = (from allItems in db.Todos select allItems); //Bind the result to the Gridview TodoGridView.DataSource = TodoItems.AsQueryable().OrderBy(SortString).ToList(); TodoGridView.DataBind(); } }
protected void TodoGridView_RowDeleting(object sender, GridViewDeleteEventArgs e) { //Store which row was selected for deletion int selectedRow = e.RowIndex; //Get the selected StudentID using the Grid's DataKEy collection int TodoID = Convert.ToInt32(TodoGridView.DataKeys[selectedRow].Values["TodoID"]); //Use EF to find the selected student in the DB and remove it using (TodoConnection db = new TodoConnection()) { //Create object of the student class and store the query string inside of it Todo deletedTodoItem = (from studentRecords in db.Todos where studentRecords.TodoID == TodoID select studentRecords).FirstOrDefault(); //Remove the selected student from the db db.Todos.Remove(deletedTodoItem); //Save changes back to the db db.SaveChanges(); //Refresh the grid this.GetTodoList(); } }