C# datagrid 分页问题

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C# datagrid 分页问题相关的知识,希望对你有一定的参考价值。

我用datagrid 的
<FooterTemplate/>中的文本来计算该列数值总和(total),可是total的值只是当前页该列的总和。我想要所有页的综合。该怎么做,高手指点。
我的代码如下:
private void dg_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)

dg.CurrentPageIndex=e.NewPageIndex;
bind();

public void CalcTotal(string _price)

total += double.Parse(_price);



private void dg_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)

if (e.Item.ItemType == ListItemType.Item||e.Item.ItemType == ListItemType.AlternatingItem)

CalcTotal( e.Item.Cells[5].Text );
e.Item.Cells[5].Text = string.Format("0:c", Convert.ToDouble(e.Item.Cells[5].Text));

else if(e.Item.ItemType == ListItemType.Footer )

e.Item.Cells[5].Text = "小计:"+string.Format("0:c", total);


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table id="Table1" style="Z-INDEX: 101; LEFT: 32px; WIDTH: 752px; POSITION: absolute; TOP: 16px; HEIGHT: 312px" cellspacing="0" cellpadding="0" border="0">
<tr>
<td style="HEIGHT: 29px"><font face="宋体">DataList分页技术和超级链接</font></td>
</tr>
<tr>
<td style="HEIGHT: 252px">
<asp:datalist id="DataList1" runat="server" Width="576px" Height="96px">
<HeaderTemplate>
定单编号<td>
员工编号<td>
定单日期<td>
运费<td>
运往所在城市
</HeaderTemplate>

<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem,"OrderID")%> <td>
<%# DataBinder.Eval(Container.DataItem,"CustomerID")%> <td>
<%# DataBinder.Eval(Container.DataItem,"OrderDate")%> <td>
<%# DataBinder.Eval(Container.DataItem,"Freight")%> <td>
<%# DataBinder.Eval(Container.DataItem,"ShipCity")%>
</ItemTemplate>
</asp:datalist>
</td>
</tr>
<tr>
<td><font face="宋体">
<asp:linkbutton id="FirstLB" runat="server" OnCommand="LinkButton_Click" CommandName="first">第一页</asp:linkbutton> 
<asp:linkbutton id="PreviousLB" runat="server" OnCommand="LinkButton_Click" CommandName="prev">上一页</asp:linkbutton> 
<asp:linkbutton id="NextLB" runat="server" OnCommand="LinkButton_Click" CommandName="next">下一页</asp:linkbutton> 
<asp:linkbutton id="EndLB" runat="server" OnCommand="LinkButton_Click" CommandName="end">最后一页</asp:linkbutton>  
总<asp:label id="TotalLbl" runat="server"></asp:label>页 当前第<asp:label id="CurrentLbl" runat="server"></asp:label>页
<asp:linkbutton id="JumpLB" runat="server" OnCommand="LinkButton_Click" CommandName="jump">跳到</asp:linkbutton>第
<asp:textbox id="TextBox1" runat="server" Width="90px"></asp:textbox>
页</font></td>
</tr>
</table>
</div>
</form>
</body>
</html>

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page

int CurrentPage;//当前页数
int PageSize; //每页条数
int PageCount; //总页数
int RecordCount;//总条数

protected void Page_Load(object sender, EventArgs e)

PageSize = 10;//每页10条记录

if (!Page.IsPostBack)

CurrentPage = 0;//当前页习惯设为0
ViewState["PageIndex"] = 0;//页索引也设为0

//计算总共有多少记录
RecordCount = CalculateRecord();

//计算总共有多少页
if (RecordCount % PageSize == 0)

PageCount = RecordCount / PageSize;

else

PageCount = RecordCount / PageSize + 1;


this.TotalLbl.Text = PageCount.ToString();//显示总页数
ViewState["PageCount"] = PageCount;//会话session 对整个 application 有效 ,而视图状态 viewstate相当于某个页面的 session

this.DataListBind();//不可以放在初始化条件之前就绑定,那样的话,如果仅有一页的数据,“下一页”页仍然显示



//计算总共有多少条记录
private int CalculateRecord()

try

int recordCount;
SqlConnection con = new SqlConnection("server=127.0.0.1;database=Northwind;uid=sa;pwd=");//数据库使用Northwind;
con.Open();

string sql = "select count(*) as count from Orders";
SqlCommand cmd = new SqlCommand(sql, con);
SqlDataReader sdr = cmd.ExecuteReader();

if (sdr.Read())

recordCount = Int32.Parse(sdr["count"].ToString());


else

recordCount = 0;


sdr.Close();
con.Close();
return recordCount;


catch (Exception ex)

throw new Exception(ex.Message);



//将数据绑定到Datalist控件
public void DataListBind()

try

int StartIndex = CurrentPage * PageSize;//设定导入的起终地址
string sql = "select * from Orders";
DataSet ds = new DataSet();
SqlConnection con = new SqlConnection("server=127.0.0.1;database=Northwind;uid=sa;pwd=");
con.Open();

SqlDataAdapter sda = new SqlDataAdapter(sql, con);
sda.Fill(ds, StartIndex, PageSize, "orders");//这是sda.Fill方法的第一次重载,里面的变量分别是数据集DataSet ,开始记录数StartRecord,最大的记录数MaxRecord,数据表名TableName
this.DataList1.DataSource = ds.Tables["orders"].DefaultView;
this.DataList1.DataBind();
this.PreviousLB.Enabled = true;
this.NextLB.Enabled = true;
if (CurrentPage == (PageCount - 1)) this.NextLB.Enabled = false;//当为最后一页时,下一页链接按钮不可用
if (CurrentPage == 0) this.PreviousLB.Enabled = false;//当为第一页时,上一页按钮不可用
this.CurrentLbl.Text = (CurrentPage + 1).ToString();//当前页数



catch (Exception ex)

throw new Exception(ex.Message);



public void LinkButton_Click(Object sender, CommandEventArgs e)//自己编写的按钮点击事件

CurrentPage = (int)ViewState["PageIndex"];//获得当前页索引
PageCount = (int)ViewState["PageCount"];//获得总页数

string cmd = e.CommandName;

//判断cmd,以判定翻页方向

switch (cmd)

case "prev"://上一页
if (CurrentPage > 0) CurrentPage--;
break;

case "next":
if (CurrentPage < (PageCount - 1)) CurrentPage++;//下一页
break;

case "first"://第一页
CurrentPage = 0;
break;

case "end"://最后一页
CurrentPage = PageCount - 1;
break;

case "jump"://跳转到第几页
if (this.TextBox1.Text.Trim() == "" || Int32.Parse(this.TextBox1.Text.Trim()) > PageCount)//如果输入数字为空或超出范围则返回

return;

else

CurrentPage = Int32.Parse(this.TextBox1.Text.ToString()) - 1;
break;


ViewState["PageIndex"] = CurrentPage;//获得当前页

this.DataListBind();//重新将DataList绑定到数据库

参考技术A 不要死从一个角落考虑,再想想别的方法吗?
你可以访问数据库,计算总的值就可以了啊,这样应该是简单的吧,其它的方法就比较麻烦!呵呵
参考技术B kfkijy

easyui的pagination分页可以不用datagrid吗

参考技术A 第一个问题:
首先我不知道你用的easyui是什么版本以及浏览器环境,这个问题的回答只是我的推测:
1、数据表格只设置了宽度而没有设置高度,并且fit属性你没有设置为true,导致数据表格的高度被内容推着往下走。
2、版本与浏览器出现兼容性问题。
3、如果觉得分页控件麻烦可以去掉。
第二个问题:
最右边这个空的地方是easyui为了防止一旦数据行太多出现垂直滚动条后为滚动条预留的宽度,默认为10,这个API上写的很清楚,叫 scrollbarSize 你可以修改这个值。
总结:
通过第二个问题看出你没有仔细看过API,其实把API看熟不用一会儿功夫,祝你好运!本回答被提问者采纳

以上是关于C# datagrid 分页问题的主要内容,如果未能解决你的问题,请参考以下文章

C# 分页中 CodeBehind 的 GridView 不起作用

C#实现分页

c# wpf的datagrid列标题间的分割线,我加background后变成一大块黑色的,怎么变成间隔的一块一块的,如图

easyui datagrid 重置分页页码

如何在 dojox.datagrid 中实现分页

为啥easyui-datagrid分页控件把所有的都遮住了?