Controls.skin
<%--
Default skin template. The following skins are provided as examples only.
1. Named control skin. The SkinId should be uniquely defined because
duplicate SkinId's per control type are not allowed in the same theme.
<asp:GridView runat="server" SkinId="gridviewSkin" BackColor="White" >
<AlternatingRowStyle BackColor="Blue" />
</asp:GridView>
2. Default skin. The SkinId is not defined. Only one default
control skin per control type is allowed in the same theme.
<asp:Image runat="server" ImageUrl="~/images/image1.jpg" />
--%>
<asp:GridView runat="server" SkinId="gridviewSkin1" BackColor="LightBlue">
<HeaderStyle BackColor="White"/>
<AlternatingRowStyle BackColor="Blue"/>
<PagerStyle BackColor="LightGreen"/>
</asp:GridView>
PeopleComparer.cs
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
/// <summary>
/// PeopleComparer is used to sort the generic collection of the People class
/// </summary>
public class PeopleComparer : IComparer<People>
{
#region Constructor
public PeopleComparer(string p_propertyName)
{
//We must have a property name for this comparer to work
this.PropertyName = p_propertyName;
}
#endregion
#region Property
private string _propertyName;
public string PropertyName
{
get { return _propertyName; }
set { _propertyName = value; }
}
#endregion
#region IComparer<People> Members
/// <summary>
/// This comparer is used to sort the generic comparer
/// The constructor sets the PropertyName that is used
/// by reflection to access that property in the object to
/// object compare.
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <returns></returns>
public int Compare(People x, People y)
{
Type t = x.GetType();
PropertyInfo val = t.GetProperty(this.PropertyName);
if (val != null)
{
return Comparer.DefaultInvariant.Compare(val.GetValue(x,null), val.GetValue(y,null));
}
else
{
throw new Exception(this.PropertyName + " is not a valid property to sort on. It doesn't exist in the Class.");
}
}
#endregion
}
People.cs
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
/// <summary>
/// Summary description for People
/// </summary>
public class People : ICloneable
{
#region Constructors
/// <summary>
/// We have a generic list of this type in our example
/// </summary>
public People()
{ }
public People(int p_key, string p_firstName, string p_lastName, string p_skills,
decimal p_dollarsPerHour, int p_age)
{
this.Key = p_key;
this.Age = p_age;
this.DollarsPerHour = p_dollarsPerHour;
this.FirstName = p_firstName;
this.LastName = p_lastName;
this.Skills = p_skills;
}
#endregion
#region Properties
private int _key;
public int Key
{
get { return _key; }
set { _key = value; }
}
private string _firstName;
public string FirstName
{
get { return _firstName; }
set { _firstName = value; }
}
private string _lastName;
public string LastName
{
get { return _lastName; }
set { _lastName = value; }
}
private string _skills;
public string Skills
{
get { return _skills; }
set { _skills = value; }
}
private decimal _dollarsPerHour;
public decimal DollarsPerHour
{
get { return _dollarsPerHour; }
set { _dollarsPerHour = value; }
}
private int _age;
public int Age
{
get { return _age; }
set { _age = value; }
}
#endregion
#region ICloneable Members
public Object Clone()
{
People people = new People();
people.Key = this.Key;
people.Age = this.Age;
people.DollarsPerHour = this.DollarsPerHour;
people.FirstName = this.FirstName;
people.LastName = this.LastName;
people.Skills = this.Skills;
return people;
}
#endregion
}
DataAccess.cs
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
/// <summary>
/// Summary description for DataAccess
/// </summary>
public class DataAccess
{
public DataAccess()
{
}
/// <summary>
/// This method is used by the object data source to get a generic list
/// of objects to bind to.
/// </summary>
/// <param name="p_sortExpression"></param>
/// <param name="p_sortDirection"></param>
/// <returns></returns>
public static List<People> GetData(string p_sortExpression, string p_sortDirection)
{
List<People> peoples = new List<People>();
//Normally here you would access your database to populate this object.
//Since I want this example not to have any need to connect to a data base
//I am just creating the data.
//Note when caching the objectdatasource a chance in parameters being
//passed in will case the select method to be called again.
#region Creating data
peoples.Add(new People(1, "Bart", "Long", "Mower", 10.00M, 18));
peoples.Add(new People(2, "Al", "Short", "Garbage Man", 18.00M, 23));
peoples.Add(new People(3, "Phil", "Whinner", "Construction", 23.50M, 22));
peoples.Add(new People(4, "Bill", "Moan", "Painter", 15.25M, 28));
peoples.Add(new People(5, "William", "Best", "Consultant", 50.00M, 30));
peoples.Add(new People(6, "Harry", "Whiler", "Student", 0M, 19));
peoples.Add(new People(7, "Grace", "Finny", "Store Clerk", 11.50M, 24));
peoples.Add(new People(8, "Mary", "Willams", "Cell Phone Sales", 19.50M, 32));
peoples.Add(new People(9, "Jim", "Green", "Teacher", 13.75M, 27));
peoples.Add(new People(10, "Bob", "RedMan", "Construction", 25.50M, 35));
peoples.Add(new People(11, "Larry", "Quinn", "Actor", 16.00M, 22));
peoples.Add(new People(12, "Alex", "Zare", "Construction", 21.25M, 21));
peoples.Add(new People(13, "Sue", "Andersen", "Waitress", 11.75M, 26));
peoples.Add(new People(14, "Rita", "Don Diego", "Home maker", 0M, 36));
peoples.Add(new People(15, "Barry", "Cline", "Repair Man", 19.50M, 40));
peoples.Add(new People(16, "Tyler", "Huge", "Teacher", 15.50M, 31));
peoples.Add(new People(17, "Martha", "Smith", "Pre-School Teacher", 12.25M, 38));
peoples.Add(new People(18, "Mark", "Dalton", "Cook", 14.50M, 35));
peoples.Add(new People(19, "George", "Larson", "Banker", 30.00M, 45));
peoples.Add(new People(20, "Tara", "Thomas", "Nurse", 22.00M, 24));
peoples.Add(new People(21, "Gary", "Black", "Doctor", 46.50M, 29));
#endregion
//We sort the generic list if requested too
if (p_sortExpression != null && p_sortExpression != string.Empty)
{
peoples.Sort(new PeopleComparer(p_sortExpression));
}
//Now that we have sorted check to see if the sort direction is desc
if (p_sortDirection != null && p_sortDirection == "Desc")
{
peoples.Reverse();
}
return peoples;
}
}
ClassDiagram.cd
<?xml version="1.0" encoding="utf-8"?>
<ClassDiagram MajorVersion="1" MinorVersion="1">
<Font Name="Tahoma" Size="8.25" />
<Class Name="People">
<Position X="0.5" Y="0.5" Width="1.5" />
<TypeIdentifier>
<FileName>App_Code/People.cs</FileName>
<HashCode>EAEAAEAAAAAAAAQABEGAAAAAAAAAAAgAAAAgAAAAAFg=</HashCode>
</TypeIdentifier>
<Compartments>
<Compartment Name="Fields" Collapsed="true" />
</Compartments>
<Lollipop Position="0.2" />
</Class>
</ClassDiagram>
PrintGridview.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
public partial class PrintGridview : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack && Session["PrintGridviewColumns"] != null &&
Session["PrintGridViewDataSource"] != null)
{
//Add the columns to the print gridview
foreach (DataControlField dcf in (DataControlFieldCollection)Session["PrintGridviewColumns"])
{
this.GridView1.Columns.Add(dcf);
}
//Set the data source and databind
this.GridView1.DataSource = Session["PrintGridViewDataSource"];
this.GridView1.DataBind();
//Do some java script to bring up the printer dialog
StringBuilder sb = new StringBuilder();
sb.Append("<script language='javascript'>");
sb.Append("window.print();");
sb.Append("</script>");
ClientScript.RegisterStartupScript(this.GetType(),"print", sb.ToString());
Session["PrintGridviewColumns"] = null;
Session["PrintGridViewDataSource"] = null;
}
}
}
PrintGridview.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="PrintGridview.aspx.cs" Inherits="PrintGridview" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Print</title>
</head>
<body>
<form id="form1" runat="server">
<table id="tbPrint" width="100%">
<tr>
<td>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
</asp:GridView>
</td>
</tr>
</table>
</form>
</body>
</html>
Default.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.Text;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ViewState["DropDownPageItems"] = 10;
}
}
protected void GridView1_PreRender(object sender, EventArgs e)
{
//This logic displays the Page # of # in the pager template
if (GridView1.TopPagerRow != null)
{
((Label)GridView1.TopPagerRow.FindControl("lbCurrentPage")).Text = (GridView1.PageIndex + 1).ToString();
((Label)GridView1.TopPagerRow.FindControl("lbTotalPages")).Text = GridView1.PageCount.ToString();
//This makes the first and last button disappear when on the first and last pages.
((LinkButton)GridView1.TopPagerRow.FindControl("lbtnFirst")).Visible = GridView1.PageIndex != 0;
((LinkButton)GridView1.TopPagerRow.FindControl("lbtnLast")).Visible = GridView1.PageCount != (GridView1.PageIndex +1);
DropDownList ddlist = (DropDownList)GridView1.TopPagerRow.FindControl("ddlPageItems");
ddlist.SelectedIndex = ddlist.Items.IndexOf(ddlist.Items.FindByValue(ViewState["DropDownPageItems"].ToString()));
}
}
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
//Since we are changing the parameters we will not use the cached
//object datasource we will call the selecte method again.
if (ObjectDataSource1.SelectParameters[0].DefaultValue == null ||
ObjectDataSource1.SelectParameters[1].DefaultValue == null ||
ObjectDataSource1.SelectParameters[0].DefaultValue != e.SortExpression ||
ObjectDataSource1.SelectParameters[1].DefaultValue == "Desc")
{ //sort direction
ObjectDataSource1.SelectParameters[1].DefaultValue = "Asce";
}
else if (ObjectDataSource1.SelectParameters[1].DefaultValue == "Asce")
{
ObjectDataSource1.SelectParameters[1].DefaultValue = "Desc";
}
//Which column to sort on.
ObjectDataSource1.SelectParameters[0].DefaultValue = e.SortExpression;
//We have to do this or we will get an this exception
//The data source 'ObjectDataSource1' does not support sorting with IEnumerable data. Automatic sorting is only supported with DataView, DataTable, and DataSet.
e.Cancel = true;
}
protected void btnPrint_Click(object sender, EventArgs e)
{
Session["PrintGridviewColumns"] = this.GridView1.Columns;
Session["PrintGridViewDataSource"] = this.ObjectDataSource1.Select();
//Do some java script to bring up the Printing form in a different browser window
StringBuilder sb = new StringBuilder();
sb.Append("<script>");
sb.Append(Environment.NewLine);
sb.Append("window.open(/"PrintGridview.aspx/",/"Print/",/"top=5,left=5/");");
sb.Append(Environment.NewLine);
sb.Append("</script>");
ClientScript.RegisterStartupScript(this.GetType(), "print", sb.ToString());
}
/// <summary>
/// I just put this in as an example of how to find an object in a generic collection
/// </summary>
/// <param name="p_key"></param>
/// <returns></returns>
private People FindByID(int p_key)
{
List<People> peoples = (List<People>)this.ObjectDataSource1.Select();
return peoples.Find(delegate(People p) {return p.Key == p_key;});
}
protected void ddlPageItems_SelectedIndexChanged(object sender, EventArgs e)
{
int pageItems = Convert.ToInt32(((DropDownList)sender).SelectedValue);
this.GridView1.PageSize = pageItems;
ViewState["DropDownPageItems"] = pageItems;
}
}
Default.aspx
<%@ Page Language="C#"
AutoEventWireup="true"
CodeFile="Default.aspx.cs"
Inherits="_Default"
Theme="ExampleTheme"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Gridview Example</title>
</head>
<body>
<form id="form1" runat="server">
<table id="tbMain" width="100%">
<tr>
<td></td>
<td align="right"><asp:Button ID="btnPrint" runat="server" OnClick="btnPrint_Click" Text="Print" /></td>
</tr>
<tr>
<td colspan="2">
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True"
AutoGenerateColumns="False" SkinID="gridviewSkin1" DataSourceID="ObjectDataSource1" OnPreRender="GridView1_PreRender" OnSorting="GridView1_Sorting">
<PagerSettings Position="Top" />
<Columns>
<asp:TemplateField SortExpression="Key" Visible="False">
<ItemTemplate>
<asp:Label ID="lbKey" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Key") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="FirstName" HeaderText="First Name" ReadOnly="True" SortExpression="FirstName" />
<asp:BoundField DataField="LastName" HeaderText="Last Name" ReadOnly="True" SortExpression="LastName" />
<asp:BoundField DataField="Skills" HeaderText="Skills" ReadOnly="True" SortExpression="Skills" />
<asp:BoundField DataField="DollarsPerHour" HeaderText="Dollars per hour" HtmlEncode="False"
ReadOnly="True" SortExpression="DollarsPerHour" DataFormatString="{0:C2}" />
<asp:BoundField DataField="Age" HeaderText="Age" ReadOnly="True" SortExpression="Age" />
</Columns>
<PagerTemplate>
<table id="tbPager" width="100%">
<tr>
<td>Page <asp:Label ID="lbCurrentPage" runat="server"></asp:Label> of <asp:Label ID="lbTotalPages" runat="server"></asp:Label></td>
<td><asp:DropDownList ID="ddlPageItems" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ddlPageItems_SelectedIndexChanged">
<asp:ListItem>5</asp:ListItem>
<asp:ListItem Selected="True">10</asp:ListItem>
<asp:ListItem>20</asp:ListItem>
</asp:DropDownList></td>
<td align="right"><asp:LinkButton ID="lbtnFirst" runat="server" CommandName="Page" CommandArgument="First" Text="<<"></asp:LinkButton>
<asp:LinkButton ID="lbtnPrev" runat="server" CommandName="Page" CommandArgument="Prev" Text="Prev"></asp:LinkButton>
<asp:LinkButton ID="lbtnNext" runat="server" CommandName="Page" CommandArgument="Next" Text="Next"></asp:LinkButton>
<asp:LinkButton ID="lbtnLast" runat="server" CommandName="Page" CommandArgument ="Last" Text=">>"></asp:LinkButton></td>
</tr>
</table>
</PagerTemplate>
<EmptyDataTemplate>
There was no data to return
</EmptyDataTemplate>
</asp:GridView>
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="GetData" TypeName ="DataAccess" CacheDuration="300" CacheExpirationPolicy="Sliding">
<SelectParameters>
<asp:Parameter Name="p_sortExpression" Type="string" Direction="input" />
<asp:Parameter Name="p_sortDirection" Type="string" Direction="input" />
</SelectParameters>
</asp:ObjectDataSource>
</td>
</tr>
</table>
</form>
</body>
</html>