注册 登录
编程论坛 ASP.NET技术论坛

笨笨姐用你教的DataGrid分页法出现问题了

shayatou 发布于 2006-05-05 16:31, 1724 次点击

下面是错误代码,请笨笨姐看看,谢谢。

当 AllowPaging 设置为真并且选定的数据源不实现 ICollection 时,AllowCustomPaging 必须为真,并且 ID 为 DataGrid1 的 DataGrid 必须设置 VirtualItemCount。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。

异常详细信息: System.Web.HttpException: 当 AllowPaging 设置为真并且选定的数据源不实现 ICollection 时,AllowCustomPaging 必须为真,并且 ID 为 DataGrid1 的 DataGrid 必须设置 VirtualItemCount。

源错误:


行 35: this.DataGrid1.DataKeyField="id";
行 36: this.DataGrid1.DataSource=dr;
行 37: this.DataGrid1.DataBind();
行 38: }
行 39:

源文件: c:\inetpub\wwwroot\mybook\admin_manage.aspx.cs 行: 37

堆栈跟踪:


[HttpException (0x80004005): 当 AllowPaging 设置为真并且选定的数据源不实现 ICollection 时,AllowCustomPaging 必须为真,并且 ID 为 DataGrid1 的 DataGrid 必须设置 VirtualItemCount。]
System.Web.UI.WebControls.DataGrid.CreateControlHierarchy(Boolean useDataSource)
System.Web.UI.WebControls.BaseDataList.OnDataBinding(EventArgs e)
System.Web.UI.WebControls.BaseDataList.DataBind()
Mybook.manage.BindToDataGrid() in c:\inetpub\wwwroot\mybook\admin_manage.aspx.cs:37
Mybook.manage.Page_Load(Object sender, EventArgs e) in c:\inetpub\wwwroot\mybook\admin_manage.aspx.cs:25
System.Web.UI.Control.OnLoad(EventArgs e)
System.Web.UI.Control.LoadRecursive()
System.Web.UI.Page.ProcessRequestMain()

14 回复
#2
shayatou2006-05-05 16:39


private void Page_Load(object sender, System.EventArgs e)

{

if (!this.IsPostBack)

{

DataGridBind();

}

}

private void DataGridBind()

{

OleDbConnenction con = new OleDbConnenction(连接access数据库串);
con.open();
OleDbCommand cmd=new OleDbCommand("select * from test",con);
OleDbDataReader dr=cmd.excuder......
this..datasource=dr;
this.DataGrid1.databind();

}
private void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)

{

this.DataGrid1.CurrentPageIndex = e.NewPageIndex;

DataGridBind();

}

#3
小笨笨2006-05-05 17:38

OleDbDataReader dr=cmd.excuder......
this..datasource=dr;
this.DataGrid1.databind();

我可没有教过用OleDbDataReader分页啊。OleDbDataReader是不能分页、排序的。如果要分页、排序的话,一般都是用dataview来做的。

#4
lanjinbo2006-05-06 00:13
DataGrid 可以自动分页的啊,在它的“属性”里面有个分页选项,只要选中就可以自己实现分页功能了。
#5
jgh80412006-06-07 10:26
难道用了OleDbDataReader,就不能使用DataGrid分页功能了吗?
那为什么属性生成器里仍有该项目.还可用.
使用了DataView就不能再用DataGrid了吗.
用DataView怎么做,能否再指点一下.
#6
小笨笨2006-06-07 14:16
把OleDbDataReader换成DataView就行了。
#7
jgh80412006-06-08 07:00

不行,提示:
error CS0029: 无法将类型“System.Data.OleDb.OleDbDataReader”隐式转换为“System.Data.DataView”

代码:
private void Binding()

{
string connstr="Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+MapPath("Aspnet.mdb")+";";
OleDbConnection Conn=new OleDbConnection(connstr);
string strCom;
strCom="SELECT PostID,Subject,UserInfo.UserName,Newpost.RegTime,Newpost.WebSite,Email FROM Newpost,UserInfo where Newpost.UserName=UserInfo.UserName ORDER BY Newpost.RegTime DESC";
OleDbCommand formCommand=new OleDbCommand(strCom,Conn);
Conn.Open();
DataView formReader=formCommand.ExecuteReader();
DG.DataSource=formReader;
DG.DataBind();
Conn.Close();


}

#8
jgh80412006-06-08 07:03
强制类型转换也不行.
#9
daisycutter2006-06-10 15:40

首先在DataGrid控件定义上加一个AllowPaging属性,将其设置为True(默认为False)
<asp:DataGird id="..." runat="server"
…………………
AllowPaging="True"
……………………
</asp:DataGird>

public void Page_Load(Object sender,EventArgs e)

{
string connstr="Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+MapPath("Aspnet.mdb")+";";
OleDbConnection Conn=new OleDbConnection(connstr);
Conn.Open();
}
if(!Page.IsPostBack)
{
BindGrid();
}

ICollection CreateTable()
{
string strCom="SELECT PostID,Subject,UserInfo.UserName,Newpost.RegTime,Newpost.WebSite,Email FROM Newpost,UserInfo
where Newpost.UserName=UserInfo.UserName ORDER BY Newpost.RegTime DESC";
DataSet ds=new DataSet();
OleDbDataAdapter MyAdapter=new OleDbDataAdapter(strCom,Conn);

MyAdapter.Fill(ds,"Newpost");
Return ds.Table["Newpost"].DefaultView;
}

private void Binding()
{
DG.DataSource=CreateTable();
DG.DataBind();
}





#10
daisycutter2006-06-10 15:46
添加OnPageIndexChanged

public void DataGrid_PageChanged(Object sender,DataGridPageChangedEventArgs e)
{
DG.CurrentPageIndex=e.NewPageIndex;
BindGrid();
}
#11
chy7102006-06-12 09:36
DataRead这个数据源不支分页功能!!!

偶一直用AapNetPage这个控件来分页,不知各位有没有用过?
#12
wrcps2006-06-15 19:36
AapNetPage
这有用过。可是有好多功能不知怎么用啊!
只会一点点啊
#13
water1234562006-06-16 07:03
以下是引用小笨笨在2006-5-5 17:38:00的发言:

OleDbDataReader dr=cmd.excuder......
this..datasource=dr;
this.DataGrid1.databind();

我可没有教过用OleDbDataReader分页啊。OleDbDataReader是不能分页、排序的。如果要分页、排序的话,一般都是用dataview来做的。

dataview这个能分页吗?我平时都是用的datalist和repeater分页,自写分页,你可以给我们发一段代码关于dataview分页吗?

#14
小笨笨2006-06-16 12:12

http://smallfools.blog.hexun.com/3137006_d.html

这是最简单的用dataview分页的方法了。

#15
dqwit2009-10-10 09:58
顶啊
1