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

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

后台代码:

复制代码 代码如下:

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" />

时间: 2024-12-21 22:03:37

Asp.net GridView使用大全(分页实现)_实用技巧的相关文章

ASP.NET GridView的Bootstrap分页样式_实用技巧

本文实例为大家分享了GridView的Bootstrap分页样式,供大家参考,具体内容如下 Revenue.cs收入类,包括实体模型和业务逻辑 public class Revenue { public Revenue(string country, string revenue, string salesmanager, string year) { this.country = country; this.revenue = revenue; this.salesmanager = sale

Asp.Net实现的通用分页函数_实用技巧

本文实例讲述了Asp.Net实现的通用分页函数.分享给大家供大家参考,具体如下: 功能: 1.每页设置显示9页,超过9页,点5页后的+1页显示(可以随便修改) 2.CSS样式自己可以设置 3.无任何咋代码产生,利于搜索引擎优化 分页程序 objPDS = new PagedDataSource(); objPDS.DataSource = dtTable.DefaultView;//绑定数据源 objPDS.AllowPaging = true; objPDS.PageSize =10;//分页

asp.net控件DataList分页用法_实用技巧

本文实例讲述了asp.net控件DataList分页用法.分享给大家供大家参考,具体如下: protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { ViewState["Page"] = 0; Bangding(); } } //绑定数据 public void Bangding() { PagedDataSource pds = new PagedDataSource(); pds.DataS

asp.net Linq TO Sql 分页方法_实用技巧

分页方法 复制代码 代码如下: /// <summary> /// /// </summary> /// <typeparam name="T"></typeparam> /// <param name="replist">控件ID</param> /// <param name="DataSource">数据源</param> /// <par

asp.net GridView导出到Excel代码_实用技巧

复制代码 代码如下: StringBuilder sb = new StringBuilder(); StringWriter sw = new StringWriter(sb); HtmlTextWriter htw = new HtmlTextWriter(sw); Page page = new Page(); HtmlForm form = new HtmlForm(); GVLinkman.EnableViewState = false; page.EnableEventValidat

asp.net Gridview里添加汇总行_实用技巧

if (e.Row.RowType == DataControlRowType.DataRow) { totalcash += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "cash")); totalunbalanced += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "unbalanced")); totalsettled += Convert.To

GridView分页的实现(通用分页模板)_实用技巧

要在GridView中加入 //实现分页 AllowPaging="true" //一页数据10行 PageSize="10" // 分页时触发的事件 OnPageIndexChanging="gvwDesignationName_PageIndexChanging" 在服务器事件里 复制代码 代码如下: protectedvoid gvwDesignationName_PageIndexChanging(object sender, GridV

C#精髓 GridView72大绝技 学习gridview的朋友必看_实用技巧

快速预览:GridView无代码分页排序GridView选中,编辑,取消,删除GridView正反双向排序GridView和下拉菜单DropDownList结合GridView和CheckBox结合鼠标移到GridView某一行时改变该行的背景色方法一鼠标移到GridView某一行时改变该行的背景色方法二GridView实现删除时弹出确认对话框GridView实现自动编号GridView实现自定义时间货币等字符串格式GridView实现用"..."代替超长字符串GridView一般换行

Repeater控件与PagedDataSource结合实现分页功能_实用技巧

本文讲解Repeater控件与PagedDataSource相结合实现其分页功能.PagedDataSource 类封装那些允许数据源控件(如 DataGrid.GridView)执行分页操作的属性.如果控件开发人员需对自定义数据绑定控件提供分页支持,即可使用此类. PagedDataSource 类的部分公共属性: AllowCustomPaging // 获取或设置指示是否启用自定义分页的值. AllowPaging // 获取或设置指示是否启用分页的值. Count // 获取要从数据源使