急,DataGrid分页问题!!请教各位!!

limop 2004-11-23 08:47:24
private void Page_Load(object sender, System.EventArgs e)
{

// 在此处放置用户代码以初始化页面
BindGrid();


}

public void BindGrid()
{
conn.open();
DataSet dataSet1;
OleDbDataAdapter my=new OleDbDataAdapter("select * from guest",conn.dbconn);
dataSet1 = new DataSet ( ) ;
my.Fill (dataSet1, "guest" ) ;
//以SqlDataAdapter实例来填充本地DataSet数据集
DataView dataView1 = dataSet1.Tables [ "guest" ].DefaultView ;
DataGrid.DataSource = dataView1 ;
DataGrid.DataBind ( ) ;
// 在此处放置用户代码以初始化页面
conn.close();
ShowStats();
}

private void ShowStats()
{
lblCurrentIndex.Text = "第 " + (DataGrid.CurrentPageIndex + 1).ToString() + " 页";
lblPageCount.Text = "总共 " + DataGrid.PageCount.ToString() + " 页";
}

public void DataGrid_Page(object sender, DataGridPageChangedEventArgs e)
{
int startIndex ;
startIndex = DataGrid.CurrentPageIndex * DataGrid.PageSize;
DataGrid.CurrentPageIndex = e.NewPageIndex;
BindGrid();
ShowStats();
}

public void PagerButtonClick(object sender, EventArgs e)
{
string arg = ((LinkButton)sender).CommandArgument.ToString();
switch(arg)
{
case "next":
if (DataGrid.CurrentPageIndex < (DataGrid.PageCount - 1))
{
DataGrid.CurrentPageIndex += 1;
}
break;
case "prev":
if (DataGrid.CurrentPageIndex > 0)
{
DataGrid.CurrentPageIndex -= 1;
}
break;
case "last":
DataGrid.CurrentPageIndex = (DataGrid.PageCount - 1);
break;
default:
DataGrid.CurrentPageIndex = System.Convert.ToInt32(arg);
break;
}
BindGrid();
ShowStats();
}

以上是我从这个论坛拷贝的源文件改了改,除了能计算页数外,其他的都不行。比如单击某一页的时候没有反映,但是我用自带的分页工具就可以实现,是不是外加分页代码和自带的分页代码有冲突啊!
请教各位,有源文件吗?或帮我改一下上面的代码!!!多谢了。
...全文
133 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
valu 2004-11-24
  • 打赏
  • 举报
回复
前台文件有没有设置好。
limop 2004-11-23
  • 打赏
  • 举报
回复
谁能把我的那段改一下啊,我不知道错在哪儿啊!!!
xtmyname 2004-11-23
  • 打赏
  • 举报
回复
这段代码在MSDN里就可以找到。。
下面是我从MSDN里复制出来的。

int startIndex = 0;

ICollection CreateDataSource()
{

// Create sample data for the DataGrid control.
DataTable dt = new DataTable();
DataRow dr;

// Define the columns of the table.
dt.Columns.Add(new DataColumn("IntegerValue", typeof(Int32)));
dt.Columns.Add(new DataColumn("StringValue", typeof(string)));
dt.Columns.Add(new DataColumn("DateTimeValue", typeof(string)));
dt.Columns.Add(new DataColumn("BoolValue", typeof(bool)));

// Populate the table with sample values. When using custom paging,
// a query should only return enough data to fill a single page,
// beginning at the start index.
for (int i = startIndex; i < (startIndex + MyDataGrid.PageSize); i++)
{
dr = dt.NewRow();

dr[0] = i;
dr[1] = "Item " + i.ToString();
dr[2] = DateTime.Now.ToShortDateString();
dr[3] = (i % 2 != 0) ? true : false;

dt.Rows.Add(dr);
}

DataView dv = new DataView(dt);
return dv;

}

void Page_Load(Object sender, EventArgs e)
{

// Load sample data only once, when the page is first loaded.
if (!IsPostBack)
{

// Set the virtual item count, which specifies the total number
// items displayed in the DataGrid control when custom paging
// is used.
MyDataGrid.VirtualItemCount = 200;

// Retrieve the segment of data to display on the page from the
// data source and bind it to the DataGrid control.
BindGrid();

}

}

void MyDataGrid_Page(Object sender, DataGridPageChangedEventArgs e)
{

// For the DataGrid control to navigate to the correct page when
// paging is allowed, the CurrentPageIndex property must be updated
// programmatically. This process is usually accomplished in the
// event-handling method for the PageIndexChanged event.

// Set CurrentPageIndex to the page the user clicked.
MyDataGrid.CurrentPageIndex = e.NewPageIndex;

// Calculate the index of the first item to display on the page
// using the current page index and the page size.
startIndex = MyDataGrid.CurrentPageIndex * MyDataGrid.PageSize;

// Retrieve the segment of data to display on the page from the
// data source and bind it to the DataGrid control.
BindGrid();

}

void BindGrid()
{

MyDataGrid.DataSource = CreateDataSource();
MyDataGrid.DataBind();

}
xtmyname 2004-11-23
  • 打赏
  • 举报
回复
你的这些代码不全。。。
webdiyer 2004-11-23
  • 打赏
  • 举报
回复
用这个免费分页控件吧,不用那么麻烦:http://www.webdiyer.com
limop 2004-11-23
  • 打赏
  • 举报
回复
在线等待各位的回复!!!!
runninghf 2004-11-23
  • 打赏
  • 举报
回复
我也遇到了这样的问题了,关注
自带的才行,为什么啊
limop 2004-11-23
  • 打赏
  • 举报
回复
不行,单击页码还是没有反映的!!!谢谢你了!
webdiyer 2004-11-23
  • 打赏
  • 举报
回复
先把Page_Load中的BindGrid()放在if(!Page.IsPostBack)块中再试。

62,074

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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