public void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
this.DataGrid1.CurrentPageIndex=e.NewPageIndex;
//----DataGrid_DataBind();
写上绑定DataGrid的事件,例如:DataBind();
}public void DataBind()
{
DataSet ds=new DataSet();
ds.ReadXml(Server.MapPath("../bbs/xmlfile1.xml"));
this.DataGrid1.AutoGenerateColumns=false; BoundColumn MyId = new BoundColumn();
BoundColumn MyName = new BoundColumn();
BoundColumn MyAddress= new BoundColumn();
MyId.HeaderText="编码";
MyId.DataField="id"; MyName.HeaderText="姓名";
MyName.DataField="name"; MyAddress.HeaderText="联系地址";
MyAddress.DataField="address"; this.DataGrid1.Columns.AddAt(0, MyId);
this.DataGrid1.Columns.AddAt(1, MyName);
this.DataGrid1.Columns.AddAt(2, MyAddress);
this.DataGrid1.DataSource = ds.Tables["item"];
this.DataGrid1.DataBind();
}
{
this.DataGrid1.CurrentPageIndex=e.NewPageIndex;
//----DataGrid_DataBind();
写上绑定DataGrid的事件,例如:DataBind();
}public void DataBind()
{
DataSet ds=new DataSet();
ds.ReadXml(Server.MapPath("../bbs/xmlfile1.xml"));
this.DataGrid1.AutoGenerateColumns=false; BoundColumn MyId = new BoundColumn();
BoundColumn MyName = new BoundColumn();
BoundColumn MyAddress= new BoundColumn();
MyId.HeaderText="编码";
MyId.DataField="id"; MyName.HeaderText="姓名";
MyName.DataField="name"; MyAddress.HeaderText="联系地址";
MyAddress.DataField="address"; this.DataGrid1.Columns.AddAt(0, MyId);
this.DataGrid1.Columns.AddAt(1, MyName);
this.DataGrid1.Columns.AddAt(2, MyAddress);
this.DataGrid1.DataSource = ds.Tables["item"];
this.DataGrid1.DataBind();
}
private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
BindGrid();
}
}
public void BindGrid()
{
DataSet ds=new DataSet();
ds.ReadXml(Server.MapPath("../bbs/xmlfile1.xml"));
this.DataGrid1.AutoGenerateColumns=false; BoundColumn MyId = new BoundColumn();
BoundColumn MyName = new BoundColumn();
BoundColumn MyAddress= new BoundColumn();
MyId.HeaderText="编码";
MyId.DataField="id"; MyName.HeaderText="姓名";
MyName.DataField="name"; MyAddress.HeaderText="联系地址";
MyAddress.DataField="address"; this.DataGrid1.Columns.AddAt(0, MyId);
this.DataGrid1.Columns.AddAt(1, MyName);
this.DataGrid1.Columns.AddAt(2, MyAddress);
this.DataGrid1.DataSource = ds.Tables["item"];
this.DataGrid1.DataBind();
}
http://msdn.microsoft.com/library/chs/default.asp?url=/library/chs/vbcon/html/vbwlkwalkthroughdisplayingdatainlistboxesonwebformspage.asp先定义当前页:
public class WebForm1 : System.Web.UI.Page
{
private int CurrentPage;
}
页第一次运行时,要显示第一页数据:
if (!Page.IsPostBack)
{
cmdNext.Parameters["@customerid"].Value = "";
CurrentPage = 0;
FillGrid(cmdNext);
}
定位到下一页,“下一页”按钮的代码如下所示:
private void btnNext_Click(object sender, System.EventArgs e)
{
// Get the page number of the page most recently displayed
CurrentPage = (int)(ViewState["CurrentPage"]);
CurrentPage++;
// Gets the id on current page
string lastid = DataGrid1.Items[9].Cells[0].Text;
cmdNext.Parameters["@customerid"].Value = lastid;
FillGrid(cmdNext);
}
定位到上一页的代码:
private void btnPrevious_Click(object sender, System.EventArgs e)
{
btnNext.Enabled = true;
CurrentPage = (int)(ViewState["CurrentPage"]);
CurrentPage--;
if (CurrentPage >= 0)
{
string firstid;
firstid = (string)(ViewState[CurrentPage.ToString()]);
cmdPrevious.Parameters["@customerid"].Value = firstid;
FillGrid(cmdPrevious);
}
}
//if(!IsPostBack)
// {
BindGrid();
// }
//if(!IsPostBack)
// {
建立DataGrid列名;运行一次就够了。
// }
然后给DataGrid 在绑定数据。