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();
}
...全文
129 7 点赞 打赏 收藏 举报
写回复
7 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
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
大侠.快帮帮忙把

  • 打赏
  • 举报
回复
相关推荐
发帖
C#
创建于2007-09-28

10.6w+

社区成员

.NET技术 C#
申请成为版主
帖子事件
创建了帖子
2004-11-18 02:53
社区公告

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