Paged GridView: Code Behind
The code behind to demonstrate the paged GridView. The gridview events all call PageAndBind, which calls the DAL for the current, next or resorted page.
using System;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class TestPagedGridNoODS : Page
{
private FakeDAL dal;
protected void Page_Load(object sender, EventArgs e)
{
dal = new FakeDAL(StoredData); //fake data is persisted in viewstate
if (!IsPostBack)
PageAndBind(GridView1);
}
#region GridView events
//all events have to PageAndBind(GridView1)
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView gv = (GridView)sender;
gv.PageIndex = e.NewPageIndex; //simply set the page
PageAndBind(gv);
}
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
GridView gv = (GridView)sender;
gv.PageIndex = 0; //always sort to page 1
GetSortDirection(e.SortExpression); //sort direction is always ascending, so we do this manually
GridViewSortBy = e.SortExpression; //sortBy is stored so we can change direction next time
PageAndBind(gv);
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView gv = (GridView)sender;
//to get index and row
//int rowIndex = e.NewEditIndex; //here we have the row directly
//GridViewRow row = gv.Rows[rowIndex];
gv.EditIndex = e.NewEditIndex;
//you must rebind
PageAndBind(gv);
//important - turn off paging and sorting
//(do this after binding, unlike in updating and canceledit)
gv.AllowPaging = false;
gv.AllowSorting = false;
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView gv = (GridView)sender;
gv.EditIndex = -1;
gv.AllowPaging = true;
gv.AllowSorting = true;
//you must rebind last of all
PageAndBind(gv);
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridView gv = (GridView)sender;
//to get index and row
int rowIndex = e.RowIndex; //here we have the row directly
GridViewRow row = gv.Rows[rowIndex]; //the row
GetDataFromTemplatedRow(gv, rowIndex, row); //use FindControl
gv.EditIndex = -1;
gv.AllowPaging = true;
gv.AllowSorting = true;
//you must rebind last of all
PageAndBind(gv);
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
GridView gv = (GridView)sender;
//to get index and row
int rowIndex = e.RowIndex; //here we have the row directly
//GridViewRow row = gv.Rows[rowIndex]; //the row
int key = (int)gv.DataKeys[rowIndex][0];
Delete(key);
if (rowIndex == 0 && gv.Rows.Count == 1)
gv.PageIndex = gv.PageIndex - 1; //only 1 item on page- page back
PageAndBind(gv); //load the new information
}
private static string GetTextBoxText(Control row, string Id)
{ //helper to get the text from a textbox in a gridrow
TextBox txtName = row.FindControl(Id) as TextBox;
if (txtName != null) return txtName.Text;
else return String.Empty;
}
#endregion
#region Properties storing GridView data and settings
private bool GridViewSortAscending
{
get { return (ViewState["SortDirection"] == null) ? true : (bool)ViewState["SortDirection"]; }
set { ViewState["SortDirection"] = value; }
}
private string GridViewSortBy
{
get { return ViewState["SortBy"] as string ?? ""; }
set { ViewState["SortBy"] = value; }
}
private void GetSortDirection(string sortBy)
{
//swap the sort direction if sorting same column
if (GridViewSortBy != sortBy)
GridViewSortAscending = true;
else
GridViewSortAscending = !GridViewSortAscending;
}
#endregion
#region Alter to fit data type
/// <summary>
/// Gets the data in pages, then binds it to the gridview
/// </summary>
/// <param name="gv">The grid view</param>
private void PageAndBind(GridView gv)
{
IList<Object> list = new List<Object>();
int pageSize = 20;
int startRow = (gv.PageIndex * pageSize);
IList<DatedItem> data =
dal.LoadDatedItemsByPage(startRow, pageSize, GridViewSortBy, GridViewSortAscending);
//copy data into generic list
foreach (DatedItem item in data)
list.Add(item);
int count = dal.LoadDatedItemsCount(); //you could cache this
//here's the bit where we use create an ODS in code to fill the gridview with paged data
GridViewFiller.Fill(gv, list, count, pageSize);
}
private void GetDataFromTemplatedRow(GridView gv, int rowIndex, Control row)
{
//you MUST use templated columns (you should anyway to validate), so use FindControl
string name = GetTextBoxText(row, "txtName");
string date = GetTextBoxText(row, "txtDate");
int key = (int)gv.DataKeys[rowIndex][0];
UpdateData(key, name, date);
}
private void UpdateData(int key, string name, string date)
{
DatedItem di = dal.Find(key);
if (di == null) return;
di.Name = name;
di.Date = Convert.ToDateTime(date);
StoredData = dal.LoadAll(); //save the data- here we just persist to viewstate!
}
private void Delete(int key)
{
dal.Delete(key);
StoredData = dal.LoadAll(); //save the data- here we just persist to viewstate!
}
private List<DatedItem> StoredData
{
get { return ViewState["StoredData"] as List<DatedItem> ?? null; }
set { ViewState["StoredData"] = value; }
}
#endregion
}