GridView分页系列(精装版)

GridView分页系列(精装版)
GridView分页系列(精装版)

GridView分页系列(精装版)

1:GridView自带分页:GridView自带的分页,是假分页,他每次从数据库把数据全部查询出之后,通过分页的算法,进行按每页数量进行分页。

分页的属性元素:分页功能的实现就是通过对这些属性元素的操作实现的。

//this.GvShow.PageIndex 当前页的索引

//this.GvShow.PageCount 总共的页数

//this.GvShow.Rows.Count 当前页签内的gridview的行数

//this.GvShow.PageSize 每页的记录数

//this.GvShow.PageIndex*this.GvShow.rows.count + 1 行索引

设置普通的GridView分页:属性AllowPaging="True"、PageSize="2"设置分页事件

onpageindexchanging="GvShow_PageIndexChanging"

后台方法绑定:

protected void GvShow_PageIndexChanging(object sender, GridViewPageEventArgs e) {

this.GvShow.PageIndex = e.NewPageIndex;

BindView();

}

2:自定义样式的GridView自带分页:

普通的GridView自带的分页,不带样式,只是普通的1,2,3等,如果希望获取到具有其他分页样式就应该设置

<%--FirstPageText="首页" LastPageText="尾页" NextPageText="下一页" PreviousPageText="上一页"--%>

后台访问此属性的实例:

this.GvShow.PagerSettings.FirstPageText = "首页";

https://www.360docs.net/doc/c715728527.html,stPageText = "尾页";

this.GvShow.PagerSettings.NextPageText = "下一页";

this.GvShow.PagerSettings.PreviousPageText = "上一页";

this.GvShow.PagerSettings.Mode = PagerButtons.NextPreviousFirstLast;

通过属性可以设置GRIDVIEW分页的样式

3:在分页模板中自定义分页的样式,虽然微软开辟了这个模板提供给用户类似于自定义分页的功能,但这个功能完全还是基于微软的GridView自带的分页进行的,属性的Visable的属性必须是true AllowPaging="true" 与

PageSize="3"属性页都要进行相关的有效设置才可以。这种情况下的分页可以不使用onpageindexchanging="GvShow_PageIndexChanging"微软自带的分页事件,开发自己独立的分页事件与方法即可

范例:

前台代码:

BorderWidth="1px"PageSize="3"

CellPadding="1"HorizontalAlign="Center"BorderStyle="None" AllowPaging="true"

AutoGenerateColumns="False"

onpageindexchanging="GvShow_PageIndexChanging"

onrowdatabound="GvShow_RowDataBound"Height="132px"

onrowcommand="GvShow_RowCommand">

BackColor="#FFFF99">

BackColor="#EEF2F1">

Height="24px"ForeColor="Black"VerticalAlign="Middle"

BackColor="#BFD5FA">

<%----%>

<%--FirstPageText="首页" LastPageText="尾页" NextPageText="下一页" PreviousPageText="上一页"--%>

VerticalAlign="Middle">

VerticalAlign="Middle">

Text='<%#Eval("cname") %>'>

VerticalAlign="Middle">

Text='<%#Eval("username") %>'>

align="center"border="0">

首页

runat="server"CommandName="before">上一页』

下一页

尾页

页次:0/0页 

0条记录 

0条记录/页

id="cmdCheck"runat="server"Text="显示数字按钮"AutoPostBack="True" oncheckedchanged="cmdCheck_CheckedChanged">

后台代码:

namespace GridViewDemo.GridView分页系列

{

public partial class GridView自定义分页02 : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

if (!IsPostBack)

{

BindView();

InitButtons();

}

}

private void BindView()

{

DataTable dt = UserDemoAccess.GetUserSouce();

ViewState["RowCounts"] = dt.Rows.Count.ToString();

this.GvShow.DataSource = dt;;

this.GvShow.DataBind();

}

protected void GvShow_PageIndexChanging(object sender, GridViewPageEventArgs e) {

this.GvShow.PageIndex = e.NewPageIndex;

BindView();

InitButtons();

}

protected void GvShow_RowDataBound(object sender, GridViewRowEventArgs e)

{

if (e.Row.RowType == DataControlRowType.DataRow)

{

e.Row.Attributes.Add("onmouseover",

"this.setAttribute('BKC',this.style.backgroundColor);this.style.cursor='default',this.style.backgrou ndColor='#ffff99'");

e.Row.Attributes.Add("onmouseout",

"this.style.backgroundColor=this.getAttribute('BKC');");

}

//if(e.Row.RowType == DataControlRowType.Pager)

//{

// GridViewRow gr = e.Row;

// //页次-- 第几页

// Label txtNowPage = gr.FindControl("txtNowPage") as Label;

// //总页数

// Label txtAllPage = gr.FindControl("txtAllPage") as Label;

// //总记录数

// Label txtTotal = gr.FindControl("txtTotal") as Label;

// //条记录/页

// Label txtNowRed = gr.FindControl("txtNowRed") as Label;

// txtNowPage.Text = (this.GvShow.PageIndex+1).ToString();

// txtAllPage.Text = this.GvShow.PageCount.ToString();

// txtTotal.Text = ViewState["RowCounts"].ToString();

// txtNowRed.Text = this.GvShow.PageSize.ToString();

//}

}

protected void cmdCheck_CheckedChanged(object sender, EventArgs e)

{

//CheckBox cmdCheck = this.GvShow.BottomPagerRow.FindControl("cmdCheck") as CheckBox;

//if (cmdCheck.Checked)

//{

// this.GvShow.PagerSettings.Mode = PagerButtons.Numeric;

//}

}

protected void GvShow_RowCommand(object sender, GridViewCommandEventArgs e) {

//控制页签

switch(https://www.360docs.net/doc/c715728527.html,mandName)

{

case"begin":

this.GvShow.PageIndex = 0;

; break;

case"before":

if(this.GvShow.PageIndex > 0)

{

this.GvShow.PageIndex -= 1;

}

; break;

case"after":

if(this.GvShow.PageIndex < this.GvShow.PageCount - 1)

{

this.GvShow.PageIndex += 1;

}

; break;

case"end":

this.GvShow.PageIndex = this.GvShow.PageCount-1;

; break;

}

//控制按钮

InitButtons();

}

private void InitButtons()

{

//获取gridviewrows的PagerTemplate底部模板

GridViewRow gr = this.GvShow.BottomPagerRow;

LinkButton cmdbegin = gr.FindControl("cmdbegin") as LinkButton;

LinkButton cmdbefore = gr.FindControl("cmdbefore") as LinkButton;

LinkButton cmdafter = gr.FindControl("cmdafter") as LinkButton;

LinkButton cmdend = gr.FindControl("cmdend") as LinkButton;

//页次-- 第几页

Label txtNowPage = gr.FindControl("txtNowPage") as Label;

//总页数

Label txtAllPage = gr.FindControl("txtAllPage") as Label;

//总记录数

Label txtTotal = gr.FindControl("txtTotal") as Label;

//条记录/页

Label txtNowRed = gr.FindControl("txtNowRed") as Label;

txtNowPage.Text = (this.GvShow.PageIndex + 1).ToString();

txtAllPage.Text = this.GvShow.PageCount.ToString();

txtTotal.Text = ViewState["RowCounts"].ToString();

txtNowRed.Text = this.GvShow.PageSize.ToString();

cmdbegin.Enabled = false;

cmdbefore.Enabled = false;

cmdafter.Enabled = false;

cmdend.Enabled = false;

if(this.GvShow.PageCount > 1)

{

cmdbegin.Enabled = true;

cmdbefore.Enabled = true;

cmdafter.Enabled = true;

cmdend.Enabled = true;

if(this.GvShow.PageIndex == 0)

{

cmdbegin.Enabled = false;

cmdbefore.Enabled = false;

}else if(this.GvShow.PageIndex == this.GvShow.PageCount-1)

{

cmdafter.Enabled = false;

cmdend.Enabled = false;

}

}

}

}

}

此外还可以不利用GridView自带的分页模板标记可以在GridView的外部构造一个talbe与gridview对应,这样更加灵活,也应用于div中的GridView。

前台代码:

align="center"border="0">

OnCommand="PagerButton_Command">首页』

runat="server"CommandName="before"OnCommand="PagerButton_Command">上一页

下一页

尾页

页次:0/0页 

0条记录 

0条记录/页

id="cmdCheck"runat="server"Text="显示数字按钮"AutoPostBack="True" oncheckedchanged="cmdCheck_CheckedChanged">

这俩种方式的区别在于:适用内部的分页模板标记,在触发事件后,不用重新绑定GridView,这个是由GridView内部的分页机制自动完成的,而采用外部构造的方式,在执行完之后,必须重新绑定gridview,因为这个时候的pageIndex等信息已经变化,需要重新进行绑定。

后台代码:

protected void PagerButton_Command(object sender,

https://www.360docs.net/doc/c715728527.html,mandEventArgs e)

{

switch (https://www.360docs.net/doc/c715728527.html,mandName)

{

case"begin":

this.GvShow.PageIndex = 0;

; break;

case"before":

if (this.GvShow.PageIndex > 0)

{

this.GvShow.PageIndex -= 1;

}

; break;

case"after":

if (this.GvShow.PageIndex < this.GvShow.PageCount - 1)

{

this.GvShow.PageIndex += 1;

}

; break;

case"end":

this.GvShow.PageIndex = this.GvShow.PageCount - 1;

; break;

}

//在设置完页签后,将数据源重新绑定到GridView中

BindView();

InitButtons();

}

一个很好样式的假分页,功能齐全:

前台页面:

BorderWidth="1px"PageSize="3"

CellPadding="1"HorizontalAlign="Center"BorderStyle="None" AllowPaging="true"

AutoGenerateColumns="False"

onrowdatabound="GvShow_RowDataBound"Height="132px">

BackColor="#FFFF99">

BackColor="#EEF2F1">

BackColor="#BFD5FA">

VerticalAlign="Middle">

VerticalAlign="Middle">

Text='<%#Eval("cname") %>'>

VerticalAlign="Middle">

Text='<%#Eval("username") %>'>

OnCommand="PagerButton_Command">首页』

runat="server"CommandName="before"OnCommand="PagerButton_Command">上一页

下一页

尾页

页次:

id="txtNowPage"runat="server"ForeColor="red">0/0页 

0条记录 

0条记录/页

onclick="lbtnTurnNewPage_Click">

onselectedindexchanged="drpPageNumbers_SelectedIndexChanged">

后台页面:

namespace GridViewDemo.GridView分页系列

{

public partial class GridView自定义分页04 : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

if (!IsPostBack)

{

BindView();

InitButtons();

InitDrp();

}

}

private void BindView()

{

DataTable dt = UserDemoAccess.GetUserSouce();

ViewState["PageCount"] = dt.Rows.Count.ToString();

this.GvShow.DataSource = dt; ;

this.GvShow.DataBind();

}

protected void GvShow_RowDataBound(object sender, GridViewRowEventArgs e)

{

if (e.Row.RowType == DataControlRowType.DataRow)

{

e.Row.Attributes.Add("onmouseover",

"this.setAttribute('BKC',this.style.backgroundColor);this.style.cursor='default',this.style.backgrou ndColor='#ffff99'");

e.Row.Attributes.Add("onmouseout",

"this.style.backgroundColor=this.getAttribute('BKC');");

}

}

protected void PagerButton_Command(object sender,

https://www.360docs.net/doc/c715728527.html,mandEventArgs e)

{

switch (https://www.360docs.net/doc/c715728527.html,mandName)

{

case"begin":

this.GvShow.PageIndex = 0;

; break;

case"before":

if (this.GvShow.PageIndex > 0)

{

this.GvShow.PageIndex -= 1;

}

; break;

case"after":

if (this.GvShow.PageIndex < this.GvShow.PageCount - 1)

{

this.GvShow.PageIndex += 1;

}

; break;

case"end":

this.GvShow.PageIndex = this.GvShow.PageCount - 1;

; break;

}

BindView();

InitButtons();

}

private void InitButtons()

{

txtNowPage.Text = (this.GvShow.PageIndex + 1).ToString();

txtAllPage.Text = this.GvShow.PageCount.ToString();

txtTotal.Text = ViewState["PageCount"].ToString();

txtNowRed.Text = this.GvShow.PageSize.ToString();

this.cmdbegin.Enabled = false;

this.cmdbefore.Enabled = false;

this.cmdafter.Enabled = false;

this.cmdend.Enabled = false;

if (this.GvShow.PageCount > 1)

{

this.cmdbegin.Enabled = true;

this.cmdbefore.Enabled = true;

this.cmdafter.Enabled = true;

this.cmdend.Enabled = true;

if (this.GvShow.PageIndex == 0)

{

this.cmdbegin.Enabled = false;

this.cmdbefore.Enabled = false;

}

else if (this.GvShow.PageIndex == this.GvShow.PageCount - 1)

{

this.cmdafter.Enabled = false;

this.cmdend.Enabled = false;

}

}

}

protected void lbtnTurnNewPage_Click(object sender, EventArgs e)

{

try

{

int pageIndexNumber = Convert.ToInt32(this.txtTurn.Text.Trim());

if (pageIndexNumber > this.GvShow.PageCount)

{

Response.Write("越界!");

return;

else if (pageIndexNumber <= 0)

{

Response.Write("负数不灵!");

return;

}

this.GvShow.PageIndex = pageIndexNumber-1;

BindView();

InitButtons();

}

catch

{

Response.Write("请输入数字!");

return;

}

}

#region下拉列表分页跳转

protected void drpPageNumbers_SelectedIndexChanged(object sender, EventArgs e) {

DropDownList drpPageNumbers = (DropDownList)sender;

//改变GridView的页签

this.GvShow.PageIndex = Convert.ToInt32(ControlDrp);

//页签改变后,重新绑定

BindView();

InitButtons();

}

private void InitDrp()

{

this.drpPageNumbers.Items.Clear();

for (int i = 0; i

{

this.drpPageNumbers.Items.Add(new ListItem((i+1).ToString(), i.ToString()));

}

}

private String ControlDrp

{

set

{

this.drpPageNumbers.SelectedValue = value;

}

{

return this.drpPageNumbers.SelectedValue;

}

}

#endregion

}

}

一个利用 模板实现的假分页实例:(可以在绑定的时候加入dropdownlist的页面选择)

前台页面:

BorderWidth="1px"PageSize="3"

CellPadding="1"HorizontalAlign="Center"BorderStyle="None" AllowPaging="true"

AutoGenerateColumns="False"

onrowdatabound="GvShow_RowDataBound"Height="132px"

onpageindexchanging="GvShow_PageIndexChanging">

BackColor="#FFFF99">

BackColor="#EEF2F1">

Height="24px"ForeColor="Black"VerticalAlign="Middle"

BackColor="#BFD5FA">

BackColor="ActiveBorder"Font-Underline="false"/>

VerticalAlign="Middle">

VerticalAlign="Middle">

Text='<%#Eval("cname") %>'>

VerticalAlign="Middle">

[第页]

[共页]

CommandName="Page"Enabled="<%# ((GridView)Container.NamingContainer).PageIndex != 0 %>"

Text="首页">

CommandName="Page"Enabled=" <%# ((GridView)Container.NamingContainer).PageIndex != 0 %>"

Text="上一页">

CommandName="Page"Enabled=" <%# ((GridView)Container.NamingContainer).PageIndex !=

((GridView)Container.NamingContainer).PageCount - 1 %>"

Text="下一页">

CommandName="Page"Enabled=" <%# ((GridView)Container.NamingContainer).PageIndex !=

((GridView)Container.NamingContainer).PageCount - 1 %>"

Text="尾页">

Text="<%# ((GridView)Container.Parent.Parent).PageIndex + 1%>"

Width="20px">

CommandName="Page"

Text="GO">

namespace GridViewDemo.GridView分页系列

{

public partial class GridView自定义分页05 : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

if (!IsPostBack)

{

BindView();

}

}

private void BindView()

{

DataTable dt = UserDemoAccess.GetUserSouce();

this.GvShow.DataSource = dt; ;

this.GvShow.DataBind();

}

protected void GvShow_RowDataBound(object sender, GridViewRowEventArgs e)

{

if (e.Row.RowType == DataControlRowType.DataRow)

{

e.Row.Attributes.Add("onmouseover",

"this.setAttribute('BKC',this.style.backgroundColor);this.style.cursor='default',this.style.backgrou ndColor='#ffff99'");

e.Row.Attributes.Add("onmouseout",

"this.style.backgroundColor=this.getAttribute('BKC');");

}

}

protected void GvShow_PageIndexChanging(object sender, GridViewPageEventArgs e) {

GridViewRow gr = this.GvShow.BottomPagerRow;

TextBox txtNewPageIndex = gr.FindControl("txtNewPageIndex") as TextBox;

string content = CheckPageIndex();

if (content != string.Empty)

{

Show(this, content);

//如果发现异常,则重新将文本框中的值,置换成当前页签的!

txtNewPageIndex.Text = (this.GvShow.PageIndex+1).ToString();

return;

}//NewPageIndex 本页跳转点击go的时候,为-2,增加一个处理!微软自带分页中,选中页签当前页无法再点击

//此时的e.NewPageIndex默认为-2

else if (e.NewPageIndex < 0)

{

int pageIndexTemp = Convert.ToInt32(txtNewPageIndex.Text) - 1;

this.GvShow.PageIndex = pageIndexTemp;

BindView();

return;

}

this.GvShow.PageIndex = e.NewPageIndex;

BindView();

}

//protected void LinkTurn_Command(object

sender,https://www.360docs.net/doc/c715728527.html,mandEventArgs e)

//{

// if (https://www.360docs.net/doc/c715728527.html,mandArgument.ToString() == "-1")

// {

// GridViewRow gr = this.GvShow.BottomPagerRow;

// TextBox txtNewPageIndex = gr.FindControl("txtNewPageIndex") as TextBox;

// try

// {

// int pageIndex = Convert.ToInt32(txtNewPageIndex.Text);

// string content = CheckPageIndex(pageIndex, "command");

// if ( content != string.Empty)

// {

// Show(this, content);

// return;

// }

// this.GvShow.PageIndex = pageIndex - 1;

// BindView();

// }

// catch

// {

// Show(this, "只能输入数字格式!");

// return;

// }

// }

//}

private string CheckPageIndex()

{

string cotent = string.Empty;

GridViewRow gr = this.GvShow.BottomPagerRow;

TextBox txtNewPageIndex = gr.FindControl("txtNewPageIndex") as TextBox;

try

{

int pageIndexTemp = Convert.ToInt32(txtNewPageIndex.Text);

if (pageIndexTemp <= 0 || pageIndexTemp > this.GvShow.PageCount)

{

cotent = "你越位了,朋友!";

}

}

catch

{

cotent = "请输入数字!";

}

return cotent;

}

public static void Show(System.Web.UI.Page page, string msg)

{

page.ClientScript.RegisterStartupScript(page.GetType(), "message", "");

}

}

}

ASPxGridView教程

ASPxGridView 1、数据绑定 一、从左边工具箱中的数据拉出SqlDataSource, 设置你的数据连接 选择你要配置的Select语句 设置完成 二、将ASPxGridView的数据源绑定为SqlDataSource

运行程序, 2、排序和分组 ASPxGridView支持单列或者多列数据分组 分组时需要把settingBehavior.AllowGroup and SettingBehavior.AllowSort属性设置为True 用户可以拖拉列表头到分组面板,分组面板可见性控制在setting.ShowGroupPanel属性 public partial class ASPxGrid : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) ASPxGridView1.GroupBy(ASPxGridView1.Columns["dptName"]); //设置分组的列} } 或者使用分组设置 点击AspxGridView控件->右键–》列–》选中dptName 在属性中选择GroupIndex 把它值改为1 排序ASPxGridView允许用户点击列表头去对数据进行排序,设置AllowSort的属性为true. 3、过滤行 过滤行允许用户通过对单元格录入信息进行行过滤 将Settings.ShowFilterRow 属性设置为True

头过滤 列头可以显示过滤按钮,点击过滤按钮可以条用过滤下拉列表,下拉列表显示当列中的唯一值允许你使用过滤规则。通常情况下,这些按钮是隐藏的,如果要显示这么过滤按钮,必须设置Settings.ShowHeaderFilterButton 的属性为真 ASPxGridView 允许你去建立个性化过滤值,定义过滤规则和实现过滤值在一个过滤列中的下拉过滤。为了达到这个目的,我们必须使用HeaderFilterFillItems 事件。 protected void ASPxGridView1_HeaderFilterFillItems(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewHeaderFilterEventArgs e) { if (object.Equals(e.Column, ASPxGridView1.Columns["dptNo"])) { PrepareQuantityFilterItems(e); return; } } protected virtual void PrepareQuantityFilterItems(DevExpress.Web.ASPxGridView.ASPxGridViewHeaderFilterEventArgs e) { e.Values.Clear(); e.AddValue(string.Format("from {0} to {1}", 1, 2), string.Empty, string.Format("[dptNo] >= {0} and [dptNo] <= {1}", 1, 2)); } 显示结果如下

GridView使用大全

GridView无代码分页排序 GridView选中,编辑,取消,删除 GridView正反双向排序 GridView和下拉菜单DropDownList结合 GridView和CheckBox结合 鼠标移到GridView某一行时改变该行的背景色方法一 鼠标移到GridView某一行时改变该行的背景色方法二 GridView实现删除时弹出确认对话框 GridView实现自动编号 GridView实现自定义时间货币等字符串格式 GridView实现用“...”代替超长字符串 GridView一般换行与强制换行 GridView显示隐藏某一列 GridView弹出新页面/弹出新窗口 GridView固定表头(不用javascript只用CSS,2行代码,很好用) GridView合并表头多重表头无错完美版(以合并3列3行举例)GridView突出显示某一单元格(例如金额低于多少,分数不及格等)GridView加入自动求和求平均值小计 GridView数据导入Excel/Excel数据读入GridView 在对GridView编辑的时候实现自动计算 实现类似winform的点击行选中功能 GridView在编辑的时候控制编辑框的列宽。 给Gridview加入Tooltip的功能 1.GridView无代码分页排序:

1.AllowSorting设为True,aspx代码中是AllowSorting="True"; 2.默认1页10条,如果要修改每页条数,修改PageSize即可,在aspx代码中是PageSize="12"。 3.默认的是单向排序的,右击GridView弹出“属性”,选择AllowSorting为True即可。 2.GridView选中,编辑,取消,删除: 效果图: 后台代码: 你可以使用sqlhelper,本文没用。代码如下: using System; using System.Data;

GridView使用总结

GridView控件用来在表中显示数据源的值。每列表示一个字段,而每行表示一条记录。GridView控件支持下面的功能: ?绑定至数据源控件,如SqlDataSource。 ?内置排序功能 ?内置更新与删除功能 ?内置分页功能 ?内置行选择功能 ?以编程方式访问GridView对象模型以动态设置属性、处理事件等。 ?多个键字段。 ?用于超链接列的多个数据字段。 ?可以通过主题和样式进行自定义外观。 GridView控件中的每一列由一个DataControlField对象表示。 默认情况下,AutoGenerateColumns属性被设置为true,表示为数据源中的每个字段自动创建绑定字段,自动生成的绑定列字段不会添加到Columns集合中。 通过将AutoGenerateColumns属性设置为false,然后创建自定义的Columns集合,您可以手动定义列字段,而不是让GridView控件自动生成列字段。除了绑定列字段外,您还可以显示按钮列字段、复选框列字段、命令字段、超链接列字段、图像字段或基于您自己的自定义模板的列字段。 下表列出了可以使用的不同列字段类型。

自定义用户界面 您可以通过设置GridView控件的不同部分的样式属性自定义该控件的外观。下表列出了不同的样式属性。 也可以显示或隐藏控件的不同部分。下表列出控制显示或隐藏哪些部分的属性。 事件

GridView控件提供多个您可以对其进行编程的事件。这使您可以在每次发生事件时都运行一个自定义例程。下表列出了GridView控件支持的事件。 事件说明 PageIndexChanged在单击某一页导航按钮时,但在GridView控件处理分页操作之后发生。此 事件通常用于以下情形:在用户定位到该控件中的另一页之后,您需要执行 某项任务。 从10个按钮里面随便选择一个的情况 PageIndexChanging在单击某一页导航按钮时,但在GridView控件处理分页操作之前发生。此 事件通常用于取消分页操作。 RowCancelingEdit在单击某一行的??取消”按钮时,但在GridView控件退出编辑模式之前发 生。此事件通常用于停止取消操作。 RowCommand当单击GridView控件中的按钮时发生。此事件通常用于在控件中单击按钮 时执行某项任务。 RowCreated当在GridView控件中创建新行时发生。此事件通常用于在创建行时修改行 的内容。 RowDataBound在GridView控件中将数据行绑定到数据时发生。此事件通常用于在行绑定 到数据时修改行的内容。 RowDeleted在单击某一行的“删除”按钮时,但在GridView控件从数据源中删除相应记 录之后发生。此事件通常用于检查删除操作的结果。 RowDeleting在单击某一行的“删除”按钮时,但在GridView控件从数据源中删除相应记 录之前发生。此事件通常用于取消删除操作。 RowEditing发生在单击某一行的“编辑”按钮以后,GridView控件进入编辑模式之前。 此事件通常用于取消编辑操作。 RowUpdated发生在单击某一行的“更新”按钮,并且GridView控件对该行进行更新之后。 此事件通常用于检查更新操作的结果。 RowUpdating发生在单击某一行的“更新”按钮以后,GridView控件对该行进行更新之前。 此事件通常用于取消更新操作。

GridView介绍

CommandName 值 说明 “Cancel” 取消编辑操作并将GridView 控件返回为只读模式。引发RowCancelingEdit 事件。 “Delete” 删除当前记录。引发RowDeleting 和RowDeleted 事件。 “Edit” 将当前记录置于编辑模式。引发RowEditing 事件。 “Page” 执行分页操作。将按钮的CommandArgument 属性设置为“First”、“Last”、“Next”、“Prev”或页码,以指定要执行的分页操作类型。引发PageIndexChanging 和PageIndexChanged 事件。 “Select” 选择当前记录。引发SelectedIndexChanging 和SelectedIndexChanged 事件。 “Sort” 对GridView 控件进行排序。引发Sorting 和Sorted 事件。 “Update” 更新数据源中的当前记录。引发RowUpdating 和RowUpdated 事件。 再实现其中的事件即可。 也可以实现RowCommand事件,通过比对CommandName属性值实现方法,但是在其中无法获取cell[?]的值? GridView中如何控制ButtonField栏的状态? 2007-01-26 16:32 .......... ******************* protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowIndex > -1) { DataRowView rv = (DataRowView)e.Row.DataItem; if (rv["field1"].ToString() == "1") ((LinkButton)e.Row.Cells[0].Controls[0]).Enabled = false;

如何实现GridView的手动分页

如何实现GridView的手动分页 我们可以这样做。先要在Html界面中GridView中增加AllowPaging="True"接着做下面的 //在page_load事件中将GridView绑定数据库 protected void Page_Load(object sender, EventArgs e) { string sqlstr=select * from TABLE; sqlconnection conn=new sqlconnection("数据库连接字串"); DataSet mydataset = new DataSet(); SqlDataAdapter myds = new SqlDataAdapter(); myds.SelectCommand = new SqlCommand(sqlstr, conn); myds.Fill(mydataset); this.GridView1.DataSource = mydataset; mydataset.Dispose(); myds.Dispose(); conn.Close(); conn.Dispose(); GridView1.DataBind(); } //GridView有一个PageIndexChanging事件 protected void GridView1_PageIndexChanging(object sender, GridViewPag eEventArgs e) { GridView1.PageIndex = e.NewPageIndex; GridView1.DataBind(); } 这样就可以实现啦! 要使用手动分页前提是GridView没有使用数据源控件(sqldatasource.....)的时候。 请教GridView的手动分页如何实现?谢谢! 请教GridView的手动分页(自定义分页)如何实现? 比如已知有10000条数据,我要GridView的分页功能来显示数据,但是自动分页功能需要每次都取出全部的10000条数据,这样效率不高, 所以就要求只用设置GridView的分页数如1000页(每页10知),当选择第n页时才从数据库中取出对应的数据,请问怎样实现? 谢谢! https://www.360docs.net/doc/c715728527.html,结合存储过程写的通用搜索分页程序

GridView分页系列

GridView分页系列 1:GridView自带分页:GridView自带的分页,是假分页,他每次从数据库把数据全部查询出之后,通过分页的算法,进行按每页数量进行分页。 分页的属性元素:分页功能的实现就是通过对这些属性元素的操作实现的。 //this.GvShow.PageIndex 当前页的索引 //this.GvShow.PageCount 总共的页数 //this.GvShow.Rows.Count 当前页签内的gridview的行数 //this.GvShow.PageSize 每页的记录数 //this.GvShow.PageIndex*this.GvShow.rows.count + 1 行索引 设置普通的GridView分页:属性AllowPaging="True"、PageSize="2"设置分页事件onpageindexchanging="GvShow_PageIndexChanging" 后台方法绑定: protected void GvShow_PageIndexChanging(object sender, GridViewPageEventArgs e) { this.GvShow.PageIndex = e.NewPageIndex; BindView(); } 2:自定义样式的GridView自带分页: 普通的GridView自带的分页,不带样式,只是普通的1,2,3等,如果希望获取到具有其他分页样式就应该设置属性 <%--FirstPageText="首页" LastPageText="尾页" NextPageText="下一页" PreviousPageText="上一页"--%> 后台访问此属性的实例: this.GvShow.PagerSettings.FirstPageText = "首页"; https://www.360docs.net/doc/c715728527.html,stPageText = "尾页"; this.GvShow.PagerSettings.NextPageText = "下一页"; this.GvShow.PagerSettings.PreviousPageText = "上一页"; this.GvShow.PagerSettings.Mode = PagerButtons.NextPreviousFirstLast; 通过属性可以设置GRIDVIEW分页的样式 3:在分页模板中自定义分页的样式,虽然微软开辟了这个模板提供给用户类似于自定义分页的功能,但这个功能完全还是基于微软的GridView自带的分页进行的,属性的Visable的属性必须是true AllowPaging="true" 与PageSize="3"属性页都要进行相关的有效设置才可以。这种情况下的分页可以不使用onpageindexchanging="GvShow_PageIndexChanging"微软自带的分页事件,开发自己独立的分页事件与方法即可 范例: 前台代码:

GridView控件自定义分页详解

GridView控件自定义分页详解 在这里我们将用一个隐藏字段来保存这个PageIndex,即当前页码.当点击上一页时,将它的值减一,知道为0,要注意的一点这里的第一页页码是0而不是1.下面看看代码,然后我们再分析分析! 1 2 3 4 6 7 9 10 11

12 13 首页 14 上一页 15 下一页 16 尾页 17
CS文件中的代码: 1 protected void PagerButton_Click(object sender, EventArgs e) 2 { 3 int pageIndx = Convert.ToInt32(CurrentPage.Value); 4 int totals = NewsManager.GetNews(0, pageSize).TotalRecords; 5 int pages = (totals % pageSize) == 0 ? (totals / pageSize) : (totals / pageSize + 1); 6 string arg = ((LinkButton)sender).CommandArgument.ToString().ToLower(); 7 switch (arg) 8 { 9 case "prev": 10 if (pageIndx > 0) 11 { 12 pageIndx -= 1; 13 } 14 break; 15 case "next": 16 if (pageIndx < pages - 1)

winform中DataGridView实现分页功能

winform中DataGridView实现分页功能 在winform的设计中,要实现对DataGridView控件的分页功能,需要两个控件:BindingSource、BindingNavigator,根据需求可对BindingNavigator进行自由的扩展,下图的示例则是根据一般需求对分页功能的实现。红色区域是对BindingNavigator控件扩展后的效果。 具体实现过程: //窗体构造方法中定义分页所需变量: int pageSize = 0; //每页显示行数 int nMax = 0; //总记录数 int pageCount = 0; //页数=总记录数/每页显示行数 int pageCurrent = 0; //当前页号 int nCurrent = 0; //当前记录行 DataTable dtInfo = new DataTable(); //存取查询数据结果 //分页功能实现

public void InitDataSet() { //判断每页显示记录数是否为空,在初始话窗体时为真 if (txtRecordNumOfPage.Text.Trim() == "") { try { //pageSize = Convert.ToInt16(ConfigurationManager.AppSettings["PageSize"]); //设置页面行数 //读取配置文件中设置的每页显示条数 string szConfigFileName = Application.ExecutablePath + ".config"; XmlDocument doc = new XmlDocument(); doc.Load(szConfigFileName); XmlNode root = doc.SelectSingleNode("configuration"); XmlNode node = root.SelectSingleNode("appSettings/add[@key='PageSize']"); XmlElement el = node as XmlElement; pageSize = Convert.ToUInt16(el.GetAttribute("value")); } catch { } if (pageSize == 0) {

GRIDVIEW两种分页代码

第一种分页 A.aspx文件 <%@ Page Language="C#" CodeFile="真分页.aspx.cs" Inherits="_Default" %> 自定义代码实现真分页


共有记录条  每页显示
共有

ForeColor="Red">页  当前是第

首页  上一页   下一页  尾页  直接跳转到

A.aspx.cs文件 using System; using System.Data;

基于GridView控件仿EXCEL格式显示数据的网页制作

基于GridView控件仿EXCEL格式显示数据的网页制作 谭东清 海南职业技术学院海南海口570216 摘要:本文通过一个实用的网页实例,深入探讨https://www.360docs.net/doc/c715728527.html,的GridView控件模仿EXCEL格式显示数据的网页制作思路与实现过程,介绍了GridView控件在冻结窗格、合并单元格、自动编号和自定义分页控件等功能的编程技巧。 关键词GridView、冻结窗格、合并单元格、自动编号 imitation EXCEL format data Webpage making based on GridView control tan dong qing Hainan College of V ocation and Technique Hainan Haikou 570216 Abstract:In this paper, by a practical example Webpage, in-depth study https://www.360docs.net/doc/c715728527.html, GridView controls to imitate EXCEL format data Webpage making ideas and implementation process, introduces the GridView control in the freeze panes, merge cells, the automatic numbering and custom paging controls functional programming skills. Keywords:GridView control, freeze panes, merge cells, Automatic numbering 1 引言 GridView是https://www.360docs.net/doc/c715728527.html,中功能强大的数据绑定控件,它可以让用户按照自己的需要控制数据的显示格式,除了控件自身提供的排序、更新和删除数据缓存等功能外,还可以通过手动编码的形式实现GridVeiw控件的高级数据绑定。 为了更好的推进顶岗实习工作,实现对顶岗实习的有效管理,我根据经济管理学院的要求开发了一个顶岗实习管理系统,通过该系统实现对学生的顶岗实习的各项数据收集、统计和分析,提高了学生顶岗实习管理的质量。下面通过该系统中的学生实习报告查阅页面,深入解析使用GridView控件仿EXCEL格式呈现数据的方法与实现,对https://www.360docs.net/doc/c715728527.html,的开发者具有一定的参考价值。 2 网页效果 本文制作的查阅学生实习报告的网页效果如图1所示,网页浏览效果类似于EXCEL的冻结窗格功能,即将报告列表的左边两列固定,剩下的右边列内容通过滚动条移动查看,点击翻页时,数据序号自动按顺序递增编号,每一次报告标题都合并了相关的数据单元格,使得数据结构清晰,查阅非常方便。 图1 网页实例

gridview控件用法介绍

ASPxGridView属性:概述设置(Settings)

文本设置(SettingsText)

在GridView中插入新记录的方法

在GridView中插入新记录的方法(结合DetailsView) 一般情况下GridView控件不允许你插入新记录。这种情况下开发人员经常使用如下的技术增加新记录: · 在GridView的下面放置一个DetailsView控件。用户可以通过DetailsView增加新记录,然后这条新记录就会显示在GridView里。 · 通过一个超级链接使用户连接到另一个使用DetailsView增加新记录的web form。一旦记录被添加后就会返回之前的页。 这些方法都有它们自己的缺点。第一种方法占用了太多的屏幕空间,即使你增加的只是很少的记录。所以它不是“主要用于编辑,偶尔增加记录”情况下的好的选择。第二种选择需要额外创建一个web form,因为来回导航会需要向服务器发送更多的请求。 解决方案 GridView控件提供了一个被称作Empty Data Template的模板。当GridView里没有数据显示的时候这个模板就会显示出来。一般在没有数据显示的时候,这个模板会被用于显示一个给出示给用户的状态信息。但是,你也可以为了别的目的而是用它。本例中,你将使用它来给GridView增加新的记录。 新建一个Web Form示例 开始先在Visual Studio中新建一个web站点。拖拽一个SQL数据源控件并配置它以从Northwind数据库的Customers表中选择出CustomerID、CompanyName、 ContactName 和Country列。 确保选择了“高级”按钮,并且选中“生成INSERT、UPDATE和DELETE语句”复选框。

现在,在你的web form里添加一个GridView控件,设置它的DataSourceID属性为SqlDataSource1。启用这个GridView的编辑、删除和分页。在GridView的智能标签中选择“编辑列…”选项。 在GridView中增加一个ButtonField,并设置它的CommandName属性为Insert。用户通过单击插入按钮来增加新的记录。

gridview自定义分页

(2) 总页数 (3) 首页、上一页、下一页、尾页 首页 上一页 下一页 尾页 注:将上述代码放在GridView的 PageIndexChanging事件中加入如下代码 protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e) { GridView theGrid = sender as GridView; // refer to the GridView int newPageIndex = 0; if (-2 == e.NewPageIndex) { // when click the "GO" Button TextBox txtNewPageIndex = null; //GridViewRow pagerRow = theGrid.Controls[0].Controls[theGrid.Controls[0].Controls.Count -

关于实现GridView分页的功能

要在GridView中加入 //实现分页 AllowPaging="true" //一页数据10行 PageSize="10" // 分页时触发的事件 OnPageIndexChanging="gvwDesignationName_PageIndexChanging" 在服务器事件里 protected void gvwDesignationName_PageIndexChanging(object sender, GridViewPageEventArgs e) { gvwDesignationName.PageIndex=e.newIndex; bingDesignatioonName(); } 这里我给出一个通用显示分页的模板(网上搜的,自己给出注释) Code 当前第: //((GridView)Container.NamingContainer)就是为了得到当前的控件 页/共:

//得到分页页面的总数 页 //如果该分页是首分页,那么该连接就不会显示了.同时对应了自带识别的命令参数CommandArgument 首页 上一页 //如果该分页是尾页,那么该连接就不会显示了 下一页

给gridview写css样式

.GridViewStyle { border-right: 2px solid #A7A6AA; border-bottom: 2px solid #A7A6AA; border-left: 2px solid white; border-top: 2px solid white; padding: 4px; } .GridViewStyle a { color: #FFFFFF; } .GridViewHeaderStyle th { border-left: 1px solid #EBE9ED; border-right: 1px solid #EBE9ED; } .GridViewHeaderStyle { background-color: #5D7B9D; font-weight: bold; color: White; } .GridViewFooterStyle { background-color: #5D7B9D; font-weight: bold; color: White; } .GridViewRowStyle { background-color: #F7F6F3; color: #333333; } .GridViewAlternatingRowStyle { background-color: #FFFFFF; color: #284775;

} .GridViewRowStyle td, .GridViewAlternatingRowStyle td { border: 1px solid #EBE9ED; } .GridViewSelectedRowStyle { background-color: #E2DED6; font-weight: bold; color: #333333; } .GridViewPagerStyle { background-color: #284775; color: #FFFFFF; } .GridViewPagerStyle table /* to center the paging links*/ { margin: 0 auto 0 auto; } Skin file:

相关文档
最新文档