datagrid分頁問題,高手救命我啊,我快被整死了解決後高分相贈!!!!

liuxingsijia 2005-11-29 02:11:44
這個我的頁面代碼:
<asp:datagrid id="MyDataGrid" runat="server"
HorizontalAlign="Center" AlternatingItemStyle-BackColor="#eeeeee"
HeaderStyle-BackColor="#aaaadd" Font-Size="8pt"
CellSpacing="0" CellPadding="3" GridLines="Both" BorderWidth="1"
BorderColor="black" OnPageIndexChanged="MyDataGrid_Page" PagerStyle-HorizontalAlign="Right"
PagerStyle-Mode="NumericPages" PageSize="15" AllowPaging="true" AllowCustomPaging="false" AllowSorting="True" AutoGenerateColumns="False" OnItemDataBound="MyDataGrid_ItemDataBound" style="left: 55px; position: absolute; top: 66px" Width="707px">
<AlternatingItemStyle BackColor="#EEEEEE"></AlternatingItemStyle>
<HeaderStyle BackColor="#AAAADD" Font-Bold="True" HorizontalAlign="Center"></HeaderStyle>
<PagerStyle HorizontalAlign="Right" Mode="NumericPages"></PagerStyle>
<Columns>
<asp:BoundColumn HeaderText="科目編號" DataField="acct_code" >
</asp:BoundColumn>
<asp:BoundColumn HeaderText="科目名稱" DataField="acct_name" >
<HeaderStyle Width="320px" />
</asp:BoundColumn>
<asp:BoundColumn HeaderText="借方(HKD)" DataField="acct_bal_base" >
</asp:BoundColumn>
<asp:BoundColumn HeaderText="貸方(HKD)" DataField="acct_bal_fc">
</asp:BoundColumn>
<asp:BoundColumn HeaderText="外幣" DataField="acct_curr_code" >
</asp:BoundColumn>
<asp:ButtonColumn Text="選擇" CommandName="Select"></asp:ButtonColumn>
</Columns>
<SelectedItemStyle BorderStyle="None" />
</asp:datagrid>
<asp:label id="lblPageCount" runat="server" style="left: 400px; position: absolute; top: 455px"></asp:label> 
<asp:label id="lblCurrentIndex" runat="server" style="left: 462px; position: absolute; top: 455px"></asp:label>
<asp:linkbutton id="btnFirst" onclick="PagerButtonClick" runat="server"
Font-size="8pt" ForeColor="navy" CommandArgument="0" style="left: 550px; position: absolute; top: 455px"></asp:linkbutton> 
<asp:linkbutton id="btnPrev" onclick="PagerButtonClick" runat="server"
Font-size="8pt" ForeColor="navy" CommandArgument="prev" style="left: 600px; position: absolute; top: 455px"></asp:linkbutton> 
<asp:linkbutton id="btnNext" onclick="PagerButtonClick" runat="server"
Font-size="8pt" ForeColor="navy" CommandArgument="next" style="left: 650px; position: absolute; top: 455px"></asp:linkbutton> 
<asp:linkbutton id="btnLast" onclick="PagerButtonClick" runat="server"
Font-size="8pt" ForeColor="navy" CommandArgument="last" style="left: 700px; position: absolute; top: 455px"></asp:linkbutton>
一個datagrid,後面是最前一頁,後一頁,上一頁最後頁的幾個按鈕

BindGrid()

public void BindGrid()
{
Cart = new DataTable();
Cart.Columns.Add("acct_code", typeof(string));
Cart.Columns.Add("acct_name", typeof(string));
Cart.Columns.Add("acct_bal_base", typeof(double));
Cart.Columns.Add("acct_bal_fc", typeof(double));
Cart.Columns.Add("acct_curr_code", typeof(string));
row = new ReaderSql();
row.SqlString = "select chart_account.acct_code,acct_name ,acct_curr_code,sp.acct_bal_base,sp.acct_bal_fc from chart_account left join (select acct_code,acct_bal_base,acct_bal_fc from acct_beg_bal where dept_code='11' ) as sp on chart_account.acct_code=sp.acct_code where chart_account.detail_acct='1' order by chart_account.acct_code ";
SqlDataReader da2 = row.getDataSource();
bool found1 = false;
while (da2.Read())
{
found1 = true;
string acctcode = row.getValue("acct_code").ToString();
string acctname = row.getValue("acct_name").ToString();
Label4.Text = row.getValue("acct_bal_base").ToString();
string balfc = row.getValue("acct_bal_fc").ToString();
string currcode = row.getValue("acct_curr_code").ToString();
DataRow CartRow = Cart.NewRow();
CartRow[0] = acctcode;
CartRow[1] = acctname;
if (Label4.Text!= "")
{
double ii;
try
{

ii = double.Parse(Label4.Text);
}
catch
{
ii = 0;
}
if (ii >0)
{
CartRow[2] = ii;
}
else
{
if (ii < 0)
{
CartRow[3] = -ii;
}

}
}
if (Label4.Text.Trim()!="")
{
if (currcode != "HKD")
{
CartRow[4] = "(" + currcode + ")" + balfc;
}


}
Cart.Rows.Add(CartRow);
MyDataGrid.DataSource = Cart.DefaultView;
MyDataGrid.DataBind();

}
row.CloseAll();

初始華的時候調用一次BindGrid()
上面的那幾個按鈕事件中的代碼
string arg = ((LinkButton)sender).CommandArgument.ToString();
switch (arg)
{

case "next":
if (MyDataGrid.CurrentPageIndex == 0)
{


}
else
{
MyDataGrid.CurrentPageIndex = 0;
}
if (MyDataGrid.CurrentPageIndex < (MyDataGrid.PageCount - 1))
{
MyDataGrid.CurrentPageIndex += 1;
}
// MyDataGrid.CurrentPageIndex = MyDataGrid.PageCount;

break;
case "prev":
if (MyDataGrid.CurrentPageIndex > 0)
{
MyDataGrid.CurrentPageIndex -= 1;
}
break;
case "last":
MyDataGrid.CurrentPageIndex = (MyDataGrid.PageCount - 1);

break;
default:
MyDataGrid.CurrentPageIndex = System.Convert.ToInt32(arg);
break;
BindGrid()//在這裡重新榜定

運行first 和pre的時候都不會錯,因為它們的currentpageindex都為0,但是只要currentpageindex變化就不可以,BindGrid出錯,出現currentpageindex is invalid value這樣的提示,折磨死我了請賜教
...全文
189 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
ye_zi 2005-11-29
  • 打赏
  • 举报
回复
帮顶
liuxingsijia 2005-11-29
  • 打赏
  • 举报
回复
可不可以不要貼地址,就實際存在的代碼談
sunnystar365 2005-11-29
  • 打赏
  • 举报
回复
http://blog.csdn.net/sunnystar365/archive/2005/09/28/491139.aspx
liuxingsijia 2005-11-29
  • 打赏
  • 举报
回复
我用的就是這個代碼
但是我不能用這個榜定,我要對數據庫的字段進行篩選
public void BindGrid()
{
OleDbConnection myConnection = cn;
DataSet ds = new DataSet();
OleDbDataAdapter adapter = new OleDbDataAdapter("Select Title,CreateDate from Document", myConnection);
adapter.Fill(ds, "Document");
MyDataGrid.DataSource = ds.Tables["Document"].DefaultView;
MyDataGrid.DataBind();
ShowStats();
}
jxufewbt 2005-11-29
  • 打赏
  • 举报
回复
用下面的代码吧:

DataGridPaging.aspx

<%@ Page language="c#" EnableViewState = "true" Codebehind="DataGridPaging.aspx.cs"
AutoEventWireup="false" Inherits="eMeng.Exam.DataGridPaging.DataGridPaging" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<meta content="Visual Basic 7.0" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" runat="server">
<asp:datagrid id="MyDataGrid" runat="server" AutoGenerateColumns="False"
HorizontalAlign="Center" AlternatingItemStyle-BackColor="#eeeeee"
HeaderStyle-BackColor="#aaaadd" Font-Size="8pt" Font-Name="Verdana"
CellSpacing="0" CellPadding="3" GridLines="Both" BorderWidth="1"
BorderColor="black" OnPageIndexChanged="MyDataGrid_Page" PagerStyle-HorizontalAlign="Right"
PagerStyle-Mode="NumericPages" PageSize="5" AllowPaging="True">
<AlternatingItemStyle BackColor="#EEEEEE"></AlternatingItemStyle>
<HeaderStyle BackColor="#AAAADD" Font-Bold="True" HorizontalAlign="Center"></HeaderStyle>
<PagerStyle HorizontalAlign="Right" Mode="NumericPages"></PagerStyle>
<Columns>
<asp:BoundColumn HeaderText="标题" DataField="Title" HeaderStyle-Width="480px">
</asp:BoundColumn>
<asp:BoundColumn HeaderText="发表日期" DataField="CreateDate" DataFormatString="{0:yyyy-MM-dd hh:mm:ss}">
</asp:BoundColumn>
</Columns>
</asp:datagrid>
<p style="FONT-SIZE:9pt" align="center">
<asp:label id="lblPageCount" runat="server"></asp:label> 
<asp:label id="lblCurrentIndex" runat="server"></asp:label>
<asp:linkbutton id="btnFirst" onclick="PagerButtonClick" runat="server" Font-Name="verdana"
Font-size="8pt" ForeColor="navy" CommandArgument="0"></asp:linkbutton> 
<asp:linkbutton id="btnPrev" onclick="PagerButtonClick" runat="server" Font-Name="verdana"
Font-size="8pt" ForeColor="navy" CommandArgument="prev"></asp:linkbutton> 
<asp:linkbutton id="btnNext" onclick="PagerButtonClick" runat="server" Font-Name="verdana"
Font-size="8pt" ForeColor="navy" CommandArgument="next"></asp:linkbutton> 
<asp:linkbutton id="btnLast" onclick="PagerButtonClick" runat="server" Font-Name="verdana"
Font-size="8pt" ForeColor="navy" CommandArgument="last"></asp:linkbutton>
</p>
</form>
</body>
</HTML>

DataGridPaging.aspx.cs

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace eMeng.Exam.DataGridPaging
{
/// <summary>
/// DataGridPaging 的摘要说明。
/// </summary>
public class DataGridPaging : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid MyDataGrid;
protected System.Web.UI.WebControls.Label lblPageCount;
protected System.Web.UI.WebControls.Label lblCurrentIndex;
protected System.Web.UI.WebControls.LinkButton btnFirst;
protected System.Web.UI.WebControls.LinkButton btnPrev;
protected System.Web.UI.WebControls.LinkButton btnNext;
protected System.Web.UI.WebControls.LinkButton btnLast;
private OleDbConnection cn = new OleDbConnection();

private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
btnFirst.Text = "最首页";
btnPrev.Text = "前一页";
btnNext.Text = "下一页";
btnLast.Text = "最后页";
OpenDatabase();
BindGrid();
}
private void OpenDatabase()
{
cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("xxxx.mdb");
cn.Open();
}
private void ShowStats()
{
lblCurrentIndex.Text = "第 " + (MyDataGrid.CurrentPageIndex + 1).ToString() + " 页";
lblPageCount.Text = "总共 " + MyDataGrid.PageCount.ToString() + " 页";
}

public void PagerButtonClick(object sender, EventArgs e)
{
string arg = ((LinkButton)sender).CommandArgument.ToString();
switch(arg)
{
case "next":
if (MyDataGrid.CurrentPageIndex < (MyDataGrid.PageCount - 1))
{
MyDataGrid.CurrentPageIndex += 1;
}
break;
case "prev":
if (MyDataGrid.CurrentPageIndex > 0)
{
MyDataGrid.CurrentPageIndex -= 1;
}
break;
case "last":
MyDataGrid.CurrentPageIndex = (MyDataGrid.PageCount - 1);
break;
default:
MyDataGrid.CurrentPageIndex = System.Convert.ToInt32(arg);
break;
}
BindGrid();
ShowStats();
}
public void BindGrid()
{
OleDbConnection myConnection = cn;
DataSet ds = new DataSet();
OleDbDataAdapter adapter = new OleDbDataAdapter("Select Title,CreateDate from Document", myConnection);
adapter.Fill(ds, "Document");
MyDataGrid.DataSource = ds.Tables["Document"].DefaultView;
MyDataGrid.DataBind();
ShowStats();
}
public void MyDataGrid_Page(object sender, DataGridPageChangedEventArgs e)
{
int startIndex ;
startIndex = MyDataGrid.CurrentPageIndex * MyDataGrid.PageSize;
MyDataGrid.CurrentPageIndex = e.NewPageIndex;
BindGrid();
ShowStats();
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion
}
}

liuxingsijia 2005-11-29
  • 打赏
  • 举报
回复
我的問題在無法改動currentpageindex
liuxingsijia 2005-11-29
  • 打赏
  • 举报
回复
謝謝你們兩個的回復啊
我有追蹤調試的有arg數值的,switch有運行的就是BindGrid()時出錯
singlepine 2005-11-29
  • 打赏
  • 举报
回复
http://singlepine.cnblogs.com/articles/281425.html
swordragon 2005-11-29
  • 打赏
  • 举报
回复
请确认arg是否数值!!!
分页的英文是pagination,记住这个单词,因为会经常遇见。在EasyUI框架下,datagrid使用分页简单到令人发指,只需要2点:1,为table启用class=”easyui-datagrid”,这样table就成为了一个数据网格。2,为table启用pagination=”true”,表示表格启用分页功能。

62,074

社区成员

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

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

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

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