在Page_load里面添加如下代码:
if (!this.IsPostBack)
{
this.BindToDataGrid();
}
在Page_load外面添加如下:
//将数据邦定在DataGrid上
private void BindToDataGrid()
{
SqlConnection con=DB.createConnection();
SqlDataAdapter sda=new SqlDataAdapter();
sda.SelectCommand=new SqlCommand("select * from Smailinfo",con);
DataSet DS=new DataSet();
sda.Fill(DS,"emp");
this.DataGrid1.DataSource=DS.Tables["emp"];
this.DataGrid1.DataBind();
}
//在DataGrid事件中双击"PageIndexChanged"事件,添加如下代码,实现DataGrid中的分页
private void DataGrid1_PageIndexChange(object source,System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
this.DataGrid1.CurrentPageIndex=e.NewPageIndex;
this.BindToDataGrid();
}