DataGrid分页问题<怎么点下一页就看不到数据了????请帮忙看一下代码:在线等待,解决后立即给分.

lsmppp 2004-11-18 02:53:28
帮定数据:
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();
分页帮定代码:
public void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
this.DataGrid1.CurrentPageIndex=e.NewPageIndex;
DataGrid_DataBind();
}
...全文
150 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
biliboy 2004-11-18
  • 打赏
  • 举报
回复
同意楼上!
morefish 2004-11-18
  • 打赏
  • 举报
回复

//if(!IsPostBack)
// {
建立DataGrid列名;运行一次就够了。
// }
然后给DataGrid 在绑定数据。


lsmppp 2004-11-18
  • 打赏
  • 举报
回复
楼上:我把这样设置还可以
//if(!IsPostBack)
// {
BindGrid();
// }
迷失在太空 2004-11-18
  • 打赏
  • 举报
回复
请参看以下代码,来源:
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);
}
}
netzww 2004-11-18
  • 打赏
  • 举报
回复

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();
}
roczou 2004-11-18
  • 打赏
  • 举报
回复
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();
}
lsmppp 2004-11-18
  • 打赏
  • 举报
回复
大侠.快帮帮忙把

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧