gridview分页的实现(winform datagridview 分页)
ASP.NET中 gridview 结合 sqldatasource 使用控件 AspNetPaper 进行分页显示。
aspx:
%@ Page Language="C#" AutoEventWireup="true" CodeFile="warn_add.aspx.cs" Inherits="System_warn_add" %
!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ""
html xmlns=""
head id="Head1" runat="server"
title无标题页/title
/head
body style="background-image:url(../img/00.gif); background-repeat:no-repeat;text-align:center;"
form id="form1" runat="server"
div style="text-align:center;"
table
tr
td align="left" style="font-size: 12px; width: 520px; color: red; height: 16px"
系统设置→库存预警设置/td
/tr
/table
asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4" DataKeyNames="id" PageSize="13" Font-Size="12px"
OnPageIndexChanging="GridView1_PageIndexChanging" DataSourceID="SqlDataSource1" GridLines="None" Width="472px" ForeColor="#333333" AllowPaging="True" Height="1px" Wrap="False"
PagerSettings FirstPageText="第一页" LastPageText="最后一页"
NextPageText="下一页" PreviousPageText="上一页" /
FooterStyle BackColor="#507CD1" ForeColor="White" Font-Bold="True" /
Columns
asp:BoundField DataField="CommodityName" HeaderText="商品名称" InsertVisible="False" ReadOnly="True"
SortExpression="CommodityName"
ItemStyle HorizontalAlign="Center" Width="60px" height="15px" /
/asp:BoundField
asp:BoundField DataField="kucun" HeaderText="库存" ReadOnly="True" SortExpression="kucun" /
asp:BoundField DataField="id" Visible="False" ReadOnly="True" SortExpression="id" /
asp:BoundField DataField="Warn" HeaderText="预警值" SortExpression="Warn"
ControlStyle Width="60px" height="15px"/
ItemStyle HorizontalAlign="Center" Width="60px" height="15px" /
/asp:BoundField
asp:CommandField HeaderText="操作" ShowEditButton="True"
ItemStyle HorizontalAlign="Center" Width="60px" height="15px" /
/asp:CommandField
/Columns
RowStyle BackColor="#EFF3FB" Height="15px" Wrap="True"/
SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" /
PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" /
HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /
AlternatingRowStyle BackColor="White" /
EditRowStyle BackColor="#B5BDF0" /
/asp:GridView
asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="%$ ConnectionStrings:dbConnectionString %"
SelectCommand="SELECT [id],[CommodityName], [kucun], [Warn] FROM [tb_Commodity]"
UpdateCommand="UPDATE [tb_Commodity] SET [Warn] = @Warn WHERE [id] = @id"
UpdateParameters
asp:Parameter Name="Warn" Type="Int32" /
asp:Parameter Name="id" Type="Int32" /
/UpdateParameters
/asp:SqlDataSource
/div
/form
/body
/html
cs:using System;
using System.Data;
using System.Configuration;
using System.Collections;
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;
public partial class System_warn_add : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
if (Convert.ToString(Session["username"]) == "")
{
Response.Write("script language=javascriptalert('非法操作!请先登陆。');location='login.aspx'/script");
}
}
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
}
}
asp.net gridview分页问题(急)?
(1)写查询语句的方法
public Dataset xxx(string searchString)
{
if(判断searchString是否是空)
{
//如果为空,查询语句为string sql="select xx from xxxx"
//如果不为空,查询语句为string sql="select xx from xxxx where ……"
}
//然后执行查询语句
}
(2)将gridview绑定数据源写一个独立的方法
比如:private void Gvbind()
{
Dataset ds=new Dataset();
if(判断搜索框中是否为空)
……
//如果搜索框为空则ds=xxx(null);
//如果搜索框不为空则ds=xxx(搜索框中的文本);
然后将数据源绑定到Gridview
}
(3)接下来凡是要绑定时都调用Gvbind()方法
gridview如何分页控制显示的行数?大虾帮帮忙
这个要用2个文件,一个是在页面中设置GridView,如:PagerTemplate
table width="100%"
tr
td style="text-align:center"
当前 asp:Label ID="Lb_cur" runat="server" Text ="%# ((GridView)Container.NamingContainer).PageIndex + 1 %"
/asp:Labelspan style="font-size: 12pt"?/ /span共 asp:Label ID="Lb_count" runat="server" text="%#((GridView)Container.NamingContainer).PageCount %"/asp:Label
?页
asp:LinkButton ID="LinkButtonFirstPage" runat="server" CommandArgument="First" CommandName="Page"
Enabled="%# ((GridView)Container.NamingContainer).PageIndex != 0 %"首页/asp:LinkButton
?
asp:LinkButton ID="LinkButtonPreviousPage" runat="server" CommandArgument="Prev" CommandName="Page"
Enabled="%# ((GridView)Container.NamingContainer).PageIndex != 0 %"上一页/asp:LinkButton
?点击跳转至第
asp:TextBox ID="Tb_page" runat="server" Width="40px" Height="15px"/asp:TextBox
?页
asp:LinkButton ID="btnGo" runat="server" causesvalidation="False"
commandargument="btnGO" commandname="Page" OnClick="btnGo_Click" text="跳转" /
?
asp:LinkButton ID="LinkButtonNextPage" runat="server" CommandArgument="Next" CommandName="Page"
Enabled="%# ((GridView)Container.NamingContainer).PageIndex != ((GridView)Container.NamingContainer).PageCount - 1 %"下一页/asp:LinkButton
?
asp:LinkButton ID="LinkButtonLastPage" runat="server" CommandArgument="Last" CommandName="Page"
Enabled="%# ((GridView)Container.NamingContainer).PageIndex != ((GridView)Container.NamingContainer).PageCount - 1 %"尾页/asp:LinkButton
/td
/tr
/table
/PagerTemplate
还要再CS代码里设置GridViewRow pagerRow = GridView1.BottomPagerRow;
System.Web.UI.WebControls.TextBox Tb = (System.Web.UI.WebControls.TextBox)pagerRow.Cells[0].FindControl("Tb_page");
int iflag = GridView1.PageIndex;
if (Session["page"]!=null)
{
Session.Remove("page");
}
Session["page"] = GridView1.PageIndex;
if (Tb.Text != "")
{
int page = Convert.ToInt32(Tb.Text);
if (page 1 || page GridView1.PageCount)
{
page = 1;
}
GridView1.PageIndex = page - 1;
}
else
{
GridView1.PageIndex = iflag;
}
ret();
ret();是绑定数据的方法