DataGrid分页爱你不容易啊

对类容说几点:
1。用DataGrid分页,方法多如牛毛,我采用的是在(OnPageIndexChanged="MyGridPage")的
事件编程。
2。在LinkButton中要注意设置:(OnClick=" " CommandArgument="  ")这两个是关键,在后台代码中也要用。

把下面这段代码放在<from></from>之间

<asp:DataGrid id="dg" style="Z-INDEX: 100; LEFT: 160px; POSITION: absolute; TOP: 48px" runat="server" AutoGenerateColumns="False" PageSize="8" AllowPaging="True" Font-Size="9pt" Font-Names="Times New Roman" Width="264px" ForeColor="#006633" BackColor="#CCCCFF" BorderWidth="1px" PagerStyle-HorizontalAlign="Right" PagerStyle-Mode="NumericPages" OnPageIndexChanged="MyGridPage">
<HeaderStyle BackColor="#339999"></HeaderStyle>
<FooterStyle BackColor="#336699"></FooterStyle>
<Columns>
<asp:BoundColumn DataField="customerid" HeaderText="客户编号"></asp:BoundColumn>
<asp:BoundColumn DataField="address" HeaderText="客户地址"></asp:BoundColumn>
<asp:BoundColumn DataField="city" HeaderText="客户城市"></asp:BoundColumn>
</Columns>
<PagerStyle HorizontalAlign="Right" Mode="NumericPages"></PagerStyle>
</asp:DataGrid>
<asp:Checkbox id="chk1" style="Z-INDEX: 108; LEFT: 432px; POSITION: absolute; TOP: 200px" runat="server" Font-Size="8pt" AutoPostBack="true" Text="显示内置的页数" Font-Name="Verdana"></asp:Checkbox><asp:Label id="la" style="Z-INDEX: 102; LEFT: 160px; POSITION: absolute; TOP: 32px" runat="server"Width="120px" Font-Size="9pt" ForeColor="#8080FF"></asp:Label>
<asp:Label id="lb" style="Z-INDEX: 103; LEFT: 296px; POSITION: absolute; TOP: 32px" runat="server"
Width="120px" Font-Size="9pt" ForeColor="#8080FF"></asp:Label>
<asp:LinkButton id="LinkButton1" style="Z-INDEX: 104; LEFT: 424px; POSITION: absolute; TOP: 56px"
runat="server" OnClick="Pager" CommandArgument="0">首页</asp:LinkButton>
<asp:LinkButton id="LinkButton2" style="Z-INDEX: 105; LEFT: 424px; POSITION: absolute; TOP: 88px"
runat="server" OnClick="Pager" CommandArgument="next">下页</asp:LinkButton>
<asp:LinkButton id="LinkButton3" style="Z-INDEX: 106; LEFT: 424px; POSITION: absolute; TOP: 120px"runat="server" OnClick="Pager" CommandArgument="pre">前页</asp:LinkButton>
<asp:LinkButton id="LinkButton4" style="Z-INDEX: 107; LEFT: 424px; POSITION: absolute; TOP: 152px"runat="server" OnClick="Pager" CommandArgument="last">后页</asp:LinkButton>

接着我们的后台代码实现:
using System.Data.SqlClient ;
private void Page_Load(object sender, System.EventArgs e)
{
 BindGrid();
if (chk1.Checked )
{
dg.PagerStyle .Visible =true;
}
else
{
dg.PagerStyle .Visible =false;
}

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

 public void MyGridPage(Object sender, DataGridPageChangedEventArgs e)
  {
   //处理按下数字的方法
    dg.CurrentPageIndex = e.NewPageIndex;
    BindGrid();
    int startIndex ;
    startIndex = dg.CurrentPageIndex * dg.PageSize;  }

ICollection CreateDataSource()
  {
   string connectionString="server=JIANGHUA;user id=sa;password=; database=northwind";
   SqlConnection mycon=new SqlConnection ();
   mycon.ConnectionString =connectionString;
   string sql;
   sql="select customerid,address,city from customers order by customerid ";
   SqlDataAdapter myadp=new SqlDataAdapter (sql,mycon);
   DataSet myds =new DataSet ();
   myadp.Fill(myds);
   mycon.Close ();
     return myds.Tables[0].DefaultView;
   
           }
 public  void BindGrid()
  {
     dg.DataSource =CreateDataSource();
   dg.DataBind ();
    la.Text ="当前共有"+dg.PageCount +"页";
    lb.Text ="当前是第"+dg.CurrentPageIndex +1+"页";
  }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值