Figure 2

Figure 2 DataGrid Events
Event
Description
Init
Fired when the DataGrid is initialized. Must perform any initialization step, but cannot access view state information or other parent or child controls
ItemCreated
Fired whenever an item is created. An item can be of several types (pager, normal, alternating, header, footer, columns, and so on)
ItemDataBound
Fired whenever an item is bound to a data source. Only normal and alternating items and columns can be data-bound
Load
Fired when the DataGrid is initialized. Comes after Init and allows you to access view state information as well as other parent or child controls within the page hierarchy
PageIndexChanged
Fired when you jump to another page
SelectedIndexChanged
Fired when an item on the list is selected
Unload
Fired when the control is unloaded from memory. Must perform any final cleanup
CancelCommand
Fired whenever a button for an item is clicked with a CommandName property of cancel
DeleteCommand
Fired whenever a button for an item is clicked with a CommandName property of delete
EditCommand
Fired whenever a button for an item is clicked with a CommandName property of edit
ItemCommand
Fired whenever any button for an item is clicked
SortCommand
Fired when a column is sorted
UpdateCommand
Fired whenever a button for an item is clicked with a CommandName property of update
Figure 5 CustomPager.aspx
<%@ Import Namespace="System.Data.SQL" %>
<%@ Import Namespace="System.Data" %>
<%@ Page Language="C#" %>
<html><head>

<style>
.PagerPush {width:30px;color:black;background:white;font-family:verdana;font-size:8pt;}
.PagerText {font-family:verdana;font-size:8pt;width:30px;}
.PagerTable {border-style:solid;border-size:1px;border-color:black}
</style>

<script runat="server">
public void Page_Load(Object Sender, EventArgs e)
{
    BindData();
}

/*
    This function creates the data source whenever it is 
    necessary. If you're going to use paging, you always 
    need to reload the data source before rebinding. 
    You cannot cache the dataset in this case.
*/
private ICollection CreateDataSource()
{
    // Set up the SQL 2000 connection
    String strConn;
    strConn = "DATABASE=Northwind;SERVER=localhost;UID=sa;PWD=;";

    // Set the SQL command to run
    String strCmd = "";
    strCmd += "SELECT ";
    strCmd += " employeeid, ";
    strCmd += "titleofcourtesy + ' ' + firstname + ' ' + lastname AS 
               EmployeeName, ";
    strCmd += "title ";
    strCmd += "FROM Employees";
    
    // Execute the command and add a named table to the dataset
    SQLDataSetCommand oCMD = new SQLDataSetCommand(strCmd, strConn);

    // Add a named table to the dataset 
    DataSet oDS = new DataSet();
    oCMD.FillDataSet(oDS, "EmployeesList");
    
    // Return a given data table
    return oDS.Tables["EmployeesList"].DefaultView;
}


/*
    This function gets called whenever the user changes 
    the SQL command to fill the datagrid with new records.
*/
public void BindData()
{
    SetGridProperties();
    DataGrid1.DataSource = CreateDataSource();
    DataGrid1.DataBind();
    
    // Update pager
    PageLabel.Text = "This table has " + "<b>";
    PageLabel.Text += DataGrid1.PageCount.ToString() + 
        "</b> pages. Go to:";

}

/*
    This function sets some datagrid's properties
*/
private void SetGridProperties()
{
    DataGrid1.PagerStyle.Mode = PagerMode.NumericPages;
    DataGrid1.PagerStyle.BackColor = System.Drawing.Color.Gainsboro;
    DataGrid1.PagerStyle.PageButtonCount = 10;
    DataGrid1.PageSize = 4;    
}

/*
    This function moves through pages 
*/
void GotoButtonClick(Object sender, EventArgs e) 
{
    // Do as follows if you want to check which button has been
    // pressed. Use commandargument to know about the reason of the 
    // button
    //String btn = ((Button)sender).CommandArgument;
    
    int nPageNumber;
    try {
        nPageNumber = PageNumber.Text.ToInt32()-1;
    }
    catch {
        nPageNumber = DataGrid1.CurrentPageIndex;
    }
    
    // no less than 0
    if(nPageNumber < 0)
        nPageNumber = 0;
    
    // no more than PageCount
    if(nPageNumber > DataGrid1.PageCount - 1)
        nPageNumber = DataGrid1.PageCount-1; 

    DataGrid1.CurrentPageIndex = nPageNumber;
    BindData();
}

</script>
</head>
<body>

<%
    Session["ShowSampleBar"] = "no";
    Session["Logo"] = "../../images/expoware.gif";
    Server.Execute("../../layout.aspx");
%>

<!-- ASP.NET Pages can contain just one server-side form -->
<form id=Form1 runat="server">
<hr noshade style="height=10px" >

<!-- Show the information -->&nbsp; 
<table class=PagerTable><tr><td>

<!-- In case you want to use a custom pager hide the standard one 
    and write a brand new piece of HTML/ASP code and use <TABLE> to 
    glue it together with the datagrid -->
<asp:datagrid id=DataGrid1 runat="server" 
    CellPadding="2" 
    CellSpacing="2" 
    PagerStyle-Mode="NumericPages"
    GridLines="None" 
    BorderStyle="solid"
    BorderColor="black"
    BorderWidth="1"
    ForeColor="Black" 
    font-size="x-small" 
    font-names="Verdana" 
    width="100%" 
    PagerStyle-HorizontalAlign="Right"
    PagerStyle-Visible="False"
    AllowPaging="True" >


<property name="AlternatingItemStyle">
<asp:TableItemStyle BackColor="palegreen">
</asp:TableItemStyle>
</property>

<property name="ItemStyle">
<asp:TableItemStyle BackColor="beige">
</asp:TableItemStyle>
</property>

<property name="HeaderStyle">
<asp:TableItemStyle Font-Bold="True" ForeColor="White" BackColor="brown">
</asp:TableItemStyle>
</property>
</asp:datagrid>
</td></tr>
<tr align=right bgcolor="#cccccc"><td>
    <asp:label id="PageLabel" runat="server" 
        Font-Name="verdana" Font-size="8pt" /> 
        
    <asp:TextBox id="PageNumber" runat="server"
        CssClass= "PagerText"
        maxlength ="2"
        Text="1" />
        
    <asp:button id="gotopage" runat="server"    
        Text="Go"
        CssClass = "PagerPush"
        CommandArgument = "goto"
        OnClick = "GotoButtonClick" />

</tr></td>
</table>
</form>

</body></html>
Figure 7 Formatting Columns
Format
Description
Sample
N
Utilizes thousand separators
12,345
C
Utilizes thousand separators prefixed by the currency symbol
$12,345
D
Plain sequence of digits
12345
E
Scientific and exponential format
12,345E3
F
Truncates to the standard (or specified) number of decimals
123.45
X
Standard hexadecimal format
0x12345