欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Asp.net GridView使用大全(分页实现)

程序员文章站 2024-03-31 17:19:34
gridview自带的分页功能实现: 要实现grdview分页的功能操作如下:1、更改grdview控件的allowpaging属性为true。2、更改grdview控件...

gridview自带的分页功能实现:

要实现grdview分页的功能
操作如下:
1、更改grdview控件的allowpaging属性为true。
2、更改grdview控件的pagesize属性为 任意数值(默认为10)
3、更改grdview控件的pagesetting->mode为numeric等(默认为numeric)该属性为分页样式。
gridview属性设置好了,从页面上也能看到分页样式。

现在开始实现分页的功能:

1、在<<asp:gridview id=......>后添加,onpageindexchanging="gridview1_pageindexchanging"
2、在对应的aspx.cs中添加:
    protected void gridview1_pageindexchanging(object sender, gridviewpageeventargs e)
    {
        gridview1.pageindex = e.newpageindex;
        initpage(); //重新绑定gridview数据的函数
    }
3、
gridview1.pageindex = e.newpageindex;
完了之后再重新绑定一下gridview。

gridview添加checkbox列实现全选/全部取消功能

首先gridview编辑模版,在模板上添加checkbox控件,之后将新添加列字段转换为templatefiled

Asp.net GridView使用大全(分页实现)

后台代码:

复制代码 代码如下:

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 default5 : system.web.ui.page
{
    sqlconnection sqlcon;
    string strcon = "data source=(local);database=北风贸易;uid=sa;pwd=sa";
    protected void page_load(object sender, eventargs e)
    {
        if (!ispostback)
        {
            bind();
        }
    }
    protected void checkbox2_checkedchanged(object sender, eventargs e)
    {
        for (int i = 0; i <= gridview1.rows.count - 1; i++)
        {
            checkbox cbox = (checkbox)gridview1.rows[i].findcontrol("checkbox1");
            if (checkbox2.checked == true)
            {
                cbox.checked = true;
            }
            else
            {
                cbox.checked = false;
            }
        }
    }
    protected void button2_click(object sender, eventargs e)
    {
        sqlcon = new sqlconnection(strcon);
        sqlcommand sqlcom;
        for (int i = 0; i <= gridview1.rows.count - 1; i++)
        {
            checkbox cbox = (checkbox)gridview1.rows[i].findcontrol("checkbox1");
            if (cbox.checked == true)
            {

                string sqlstr = "delete from 飞狐工作室 where 身份证号码='" + gridview1.datakeys[i].value + "'";
                sqlcom = new sqlcommand(sqlstr, sqlcon);
                sqlcon.open();
                sqlcom.executenonquery();
                sqlcon.close();
            }
        }
        bind();
    }
    protected void button1_click(object sender, eventargs e)
    {
        checkbox2.checked = false;
        for (int i = 0; i <= gridview1.rows.count - 1; i++)
        {
            checkbox cbox = (checkbox)gridview1.rows[i].findcontrol("checkbox1");
            cbox.checked = false;
        }
    }
    public void bind()
    {
        string sqlstr = "select top 5 * from 飞狐工作室";
        sqlcon = new sqlconnection(strcon);
        sqldataadapter myda = new sqldataadapter(sqlstr, sqlcon);
        dataset myds = new dataset();
        sqlcon.open();
        myda.fill(myds, "tb_member");
        gridview1.datasource = myds;
        gridview1.datakeynames = new string[] { "身份证号码" };
        gridview1.databind();
        sqlcon.close();
    }
}


前台主要代码:
复制代码 代码如下:

<asp:gridview id="gridview1" runat="server" allowsorting="true" autogeneratecolumns="false"
                        cellpadding="3" font-size="9pt" backcolor="white" bordercolor="#cccccc" borderstyle="none" borderwidth="1px">
                        <footerstyle backcolor="white" forecolor="#000066" />
                        <columns>
                             <asp:templatefield>
                                <itemtemplate>
                                    <asp:checkbox id="checkbox1" runat="server" />
                                </itemtemplate>
                            </asp:templatefield>
                             <asp:boundfield datafield="身份证号码" headertext="用户id" sortexpression="身份证号码" />
                            <asp:boundfield datafield="姓名" headertext="用户姓名" sortexpression="姓名"/>

                            <asp:boundfield datafield="家庭住址" headertext="家庭住址" sortexpression="家庭住址"/>

                        </columns>
                        <rowstyle forecolor="#000066" />
                        <selectedrowstyle backcolor="#669999" font-bold="true" forecolor="white" />
                        <pagerstyle backcolor="white" forecolor="#000066" horizontalalign="left" />
                        <headerstyle backcolor="#006699" font-bold="true" forecolor="white" />
                    </asp:gridview>
                     <asp:checkbox id="checkbox2" runat="server" autopostback="true" font-size="9pt" oncheckedchanged="checkbox2_checkedchanged"
                        text="全选" />
                    <asp:button id="button1" runat="server" font-size="9pt" text="取消" onclick="button1_click" />
                    <asp:button id="button2" runat="server" font-size="9pt" text="删除" onclick="button2_click" />