一个关于datagrid分页的基础问题~~~~~

marist 2004-05-06 06:48:00
把数据库绑定到dropdownlist,在SelectedIndexChanged事件中编写编写,可是当调试时,选择dropdownlist的下拉菜单后,出现以下的错误:

“当 AllowPaging 设置为真并且选定的数据源不实现 ICollection 时,AllowCustomPaging 必须为真,并且 ID 为 dgdlist 的 DataGrid 必须设置 VirtualItemCount。”

代码如下:
-------------------------------------------------------------------------
private void dgdlist_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
dgdlist.CurrentPageIndex = e.NewPageIndex;
dgdlist.DataBind();
}

private void ddlrelation_SelectedIndexChanged(object sender, System.EventArgs e)
{
string strcontact_name = ddlcontact_name.SelectedItem.Text.ToString();
string strrelation = ddlrelation.SelectedItem.Value.ToString();
string strSql = "SELECT friend_name,mobil,co_phone,home_phone,email,MSN,address,post,detail FROM my_contact WHERE"+
" relation='"+strrelation+"' ORDER BY id";
string strCn = "server=xxx;database=mydb;uid=sa;pwd=xxx;";
SqlConnection ObjCn = new SqlConnection(strCn);
SqlCommand ObjCmd = new SqlCommand(strSql,ObjCn);
if(strcontact_name == "bb")
{
ObjCn.Open();
dgdlist.DataSource=ObjCmd.ExecuteReader();
dgdlist.DataBind();
ObjCn.Close();
}
-------------------------------------------------------------------------
请问VirtualItemCount是什么?应该如何设置?如何才能让下拉菜单内容绑定数据库并分页显示成功?请大家赐教,谢谢!!
...全文
137 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
vzxq 2004-05-07
  • 打赏
  • 举报
回复
帮你UP
wolftop 2004-05-07
  • 打赏
  • 举报
回复
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dv_vstechart/html/vbtchTopQuestionsAboutASPNETDataGridServerControl.asp
marist 2004-05-06
  • 打赏
  • 举报
回复
SqlCommand ObjCmd = new SqlCommand(strSql,ObjCn);
anantnt203120 2004-05-06
  • 打赏
  • 举报
回复
不知道你绑定到DataGrid上是用的什么数据集,DataAdapter不能实现分页。
marist 2004-05-06
  • 打赏
  • 举报
回复
看得不太懂,我在ddlrelation_SelectedIndexChanged()里加了两句,
dgdlist.AllowCustomPaging = true;
dgdlist.VirtualItemCount = 100;
使用下拉菜单,数据的第一页能够正常显示,可是翻到第二页,数据就不显示了,看来是没有进行数据绑定。整段代码如下:

private void dgdlist_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
dgdlist.CurrentPageIndex = e.NewPageIndex;
dgdlist.DataBind();
}

private void ddlrelation_SelectedIndexChanged(object sender, System.EventArgs e)
{
string strcontact_name = ddlcontact_name.SelectedItem.Text.ToString();
string strrelation = ddlrelation.SelectedItem.Value.ToString();
string strSql = "SELECT friend_name,mobil,co_phone,home_phone,email,MSN,address,post,detail FROM my_contact WHERE relation='"+strrelation+"' ORDER BY id";
string strCn = "server=xxx;database=mydb;uid=sa;pwd=xxx;";
SqlConnection ObjCn = new SqlConnection(strCn);
SqlCommand ObjCmd = new SqlCommand(strSql,ObjCn);
if(strcontact_name == "bbb")
{
ObjCn.Open();
dgdlist.DataSource=ObjCmd.ExecuteReader();
dgdlist.AllowCustomPaging = true;
dgdlist.VirtualItemCount = 100;
dgdlist.DataBind();
ObjCn.Close();
}

如何对分页的数据进行绑定?我第一次用datagrid的分页功能,请各位帮帮忙,谢谢!
Seeko0 2004-05-06
  • 打赏
  • 举报
回复

//如何才能让下拉菜单内容绑定数据库并分页显示成功?
//根据dgdlist.PageCount来绑定DropDownList1
private void DropDownList1_SelectedIndexChanged(object sender, System.EventArgs e)
{
dgdlist.CurrentPageIndex =DropDownList1.SelectedIndex ;//e.NewPageIndex;
dgdlist.DataBind();
}
wszl 2004-05-06
  • 打赏
  • 举报
回复
VirtualItemCount是你采用定制分页时用的,就是纪录集的条数。在下拉菜单中用不到。
AllowCustomPaging你如果不用自定义的分页就设为false

要绑定到下拉菜单中需指定绑定的字段
dgdlist.DataTextField = "friend_name";//显示的内容字段
wangsaokui 2004-05-06
  • 打赏
  • 举报
回复
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();

}

}
wangsaokui 2004-05-06
  • 打赏
  • 举报
回复
DataGrid.VirtualItemCount 属性
获取或设置在使用自定义分页时 DataGrid 控件中的实际项数。

属性值
在使用自定义分页时 DataGrid 控件中的实际项数。

备注
使用此属性来指定在使用自定义分页时 DataGrid 控件中的实际项数。仅当 AllowCustomPaging 属性设置为 true 时才使用该属性。

如果 AllowCustomPaging 属性被设置为 true,则基于 PageSize 和 VirtualItemCount 属性的值计算要显示 DataGrid 控件中每一项所需的页数。

如果 AllowCustomPaging 属性被设置为 false,则基于 PageSize 属性的值和数据源中的总项数计算 DataGrid 控件中显示的页数。

ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.2052/cpref/html/frlrfSystemWebUIWebControlsDataGridClassVirtualItemCountTopic.htm

62,244

社区成员

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

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

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

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