private
OleDbConnection con
=
new
OleDbConnection();
private void Page_Load( object sender, System.EventArgs e)
{
OpenDataBase();
BindDataGrid();
}
private void OpenDataBase()
{
con.ConnectionString = " Provider = SqlOleDb; WorkStation ID = 127.0.0.1; Initial Catalog = Northwind; User ID = sa; PassWord = ; Connect TimeOut = 30; Packet Size = 1024; Persist Security Info = false " ;
con.Open();
}
public void BindDataGrid()
{
OleDbConnection myConnection = con;
DataSet ds = new DataSet();
OleDbDataAdapter adapter = new OleDbDataAdapter( " Select LastName, FirstName, Address From Employees " , myConnection);
adapter.Fill(ds, " Document " );
dgdNorthwind.DataSource = ds.Tables[ " Document " ].DefaultView;
dgdNorthwind.DataBind();
ShowState();
}
private void ShowState()
{
lblPageCount.Text = " 总共 " + dgdNorthwind.PageCount + " 页 " ;
lblCurrentPageIndex.Text = " 第 " + (dgdNorthwind.CurrentPageIndex + 1 ).ToString() + " 页 " ;
}
private void dgdNorthwind_PageIndexChanged( object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
dgdNorthwind.CurrentPageIndex = e.NewPageIndex;
BindDataGrid();
}
private void ClickButton( object sender, EventArgs e)
{
string arg = ((LinkButton)sender).CommandArgument.ToString();
switch (arg)
{
case " Next " :
if (dgdNorthwind.CurrentPageIndex < (dgdNorthwind.PageCount - 1 ))
{
dgdNorthwind.CurrentPageIndex += 1 ;
}
break ;
case " Prev " :
if (dgdNorthwind.CurrentPageIndex > 0 )
{
dgdNorthwind.CurrentPageIndex -= 1 ;
}
break ;
case " Last " :
dgdNorthwind.CurrentPageIndex = dgdNorthwind.PageCount - 1 ;
break ;
default :
dgdNorthwind.CurrentPageIndex = 0 ;
break ;
}
BindDataGrid();
}
private void Page_Load( object sender, System.EventArgs e)
{
OpenDataBase();
BindDataGrid();
}
private void OpenDataBase()
{
con.ConnectionString = " Provider = SqlOleDb; WorkStation ID = 127.0.0.1; Initial Catalog = Northwind; User ID = sa; PassWord = ; Connect TimeOut = 30; Packet Size = 1024; Persist Security Info = false " ;
con.Open();
}
public void BindDataGrid()
{
OleDbConnection myConnection = con;
DataSet ds = new DataSet();
OleDbDataAdapter adapter = new OleDbDataAdapter( " Select LastName, FirstName, Address From Employees " , myConnection);
adapter.Fill(ds, " Document " );
dgdNorthwind.DataSource = ds.Tables[ " Document " ].DefaultView;
dgdNorthwind.DataBind();
ShowState();
}
private void ShowState()
{
lblPageCount.Text = " 总共 " + dgdNorthwind.PageCount + " 页 " ;
lblCurrentPageIndex.Text = " 第 " + (dgdNorthwind.CurrentPageIndex + 1 ).ToString() + " 页 " ;
}
private void dgdNorthwind_PageIndexChanged( object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
dgdNorthwind.CurrentPageIndex = e.NewPageIndex;
BindDataGrid();
}
private void ClickButton( object sender, EventArgs e)
{
string arg = ((LinkButton)sender).CommandArgument.ToString();
switch (arg)
{
case " Next " :
if (dgdNorthwind.CurrentPageIndex < (dgdNorthwind.PageCount - 1 ))
{
dgdNorthwind.CurrentPageIndex += 1 ;
}
break ;
case " Prev " :
if (dgdNorthwind.CurrentPageIndex > 0 )
{
dgdNorthwind.CurrentPageIndex -= 1 ;
}
break ;
case " Last " :
dgdNorthwind.CurrentPageIndex = dgdNorthwind.PageCount - 1 ;
break ;
default :
dgdNorthwind.CurrentPageIndex = 0 ;
break ;
}
BindDataGrid();
}