WebForm GridView设置分页,编辑,更新,取消,删除,按钮,以及排序。

数据库:


首先要在GridView上取消掉 自动生成列(根据T-SQL)

  1. AutoGenerateColumns="False"  
AutoGenerateColumns="False"

添加BoundField,就是绑定列,设置DataField(绑定的列),HeaderText列标题。


实际就是下面的代码:

还需要开启:编辑,更新,取消,删除(CommandField),以及设置事件:

实际就是下面的代码:

  1. <asp:GridView ID="GridView1" runat="server" AllowPaging="True"  AutoGenerateColumns="False" OnPageIndexChanging="GridView1_PageIndexChanging" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowEditing="GridView1_RowEditing" AllowSorting="True" OnRowDeleting="GridView1_RowDeleting" OnRowUpdating="GridView1_RowUpdating" OnSorting="GridView1_Sorting" >  
  2.             <Columns>  
  3.                 <asp:BoundField DataField="新闻标题" HeaderText="新闻标题" SortExpression="新闻标题" />  
  4.                 <asp:BoundField DataField="新闻内容" HeaderText="新闻内容" SortExpression="新闻内容" />  
  5.                 <asp:CommandField ShowEditButton="True" />  
  6.                 <asp:CommandField ShowDeleteButton="True" />  
  7.             </Columns>  
  8.         </asp:GridView>  
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"  AutoGenerateColumns="False" OnPageIndexChanging="GridView1_PageIndexChanging" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowEditing="GridView1_RowEditing" AllowSorting="True" OnRowDeleting="GridView1_RowDeleting" OnRowUpdating="GridView1_RowUpdating" OnSorting="GridView1_Sorting" >
            <Columns>
                <asp:BoundField DataField="新闻标题" HeaderText="新闻标题" SortExpression="新闻标题" />
                <asp:BoundField DataField="新闻内容" HeaderText="新闻内容" SortExpression="新闻内容" />
                <asp:CommandField ShowEditButton="True" />
                <asp:CommandField ShowDeleteButton="True" />
            </Columns>
        </asp:GridView>

直接上全部代码吧,时间关系:

WebForm1.aspx

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="UI.WebForm1" %>  
  2.   
  3. <!DOCTYPE html>  
  4.   
  5. <html xmlns="http://www.w3.org/1999/xhtml">  
  6. <head runat="server">  
  7. <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>  
  8.     <title></title>  
  9. </head>  
  10. <body>  
  11.     <form id="form1" runat="server">  
  12.         <div>  
  13.               
  14.         </div>  
  15.         <asp:GridView ID="GridView1" runat="server" AllowPaging="True"  AutoGenerateColumns="False" OnPageIndexChanging="GridView1_PageIndexChanging" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowEditing="GridView1_RowEditing" AllowSorting="True" OnRowDeleting="GridView1_RowDeleting" OnRowUpdating="GridView1_RowUpdating" OnSorting="GridView1_Sorting" >  
  16.             <Columns>  
  17.                 <asp:BoundField DataField="新闻标题" HeaderText="新闻标题" SortExpression="新闻标题" />  
  18.                 <asp:BoundField DataField="新闻内容" HeaderText="新闻内容" SortExpression="新闻内容" />  
  19.                 <asp:CommandField ShowEditButton="True" />  
  20.                 <asp:CommandField ShowDeleteButton="True" />  
  21.             </Columns>  
  22.         </asp:GridView>  
  23.     </form>  
  24. </body>  
  25. </html>  
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="UI.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            
        </div>
        <asp:GridView ID="GridView1" runat="server" AllowPaging="True"  AutoGenerateColumns="False" OnPageIndexChanging="GridView1_PageIndexChanging" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowEditing="GridView1_RowEditing" AllowSorting="True" OnRowDeleting="GridView1_RowDeleting" OnRowUpdating="GridView1_RowUpdating" OnSorting="GridView1_Sorting" >
            <Columns>
                <asp:BoundField DataField="新闻标题" HeaderText="新闻标题" SortExpression="新闻标题" />
                <asp:BoundField DataField="新闻内容" HeaderText="新闻内容" SortExpression="新闻内容" />
                <asp:CommandField ShowEditButton="True" />
                <asp:CommandField ShowDeleteButton="True" />
            </Columns>
        </asp:GridView>
    </form>
</body>
</html>
WebForm1.aspx.cs

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7. using BLL;  
  8. using Model;  
  9. using System.Data;  
  10. namespace UI  
  11. {  
  12.     public partial class WebForm1 : System.Web.UI.Page  
  13.     {  
  14.         protected void Page_Load(object sender, EventArgs e)  
  15.         {  
  16.             if (!IsPostBack)  
  17.             {  
  18.                 //这里给一个初始值  (ViewState是一个字典(服务器控件的一个跨页变量))  
  19.                 ViewState["SortOrder"] = "新闻标题";//排序表达式SortExpression实际就是DataField 就是T-SQL语句查询出来的列名  
  20.                 //排序表达式 实际就是设置按照哪个列进行排序而已  
  21.                 ViewState["SortDire"] = "ASC";//排序顺序(升序)  
  22.                 Bind();  
  23.             }  
  24.         }  
  25.         //把绑定数据 写到一个方法里  
  26.         private void Bind()  
  27.         {  
  28.             UserBLL ub = new UserBLL();  
  29.              
  30.             //(重点)设置一个标识(一般是主键)绑定在每一项上去,用于删除数据时,获取主键。(把主键id放在DataKeys)  
  31.             this.GridView1.DataKeyNames = new string[] { "id" };  //这个是绑定在行上的  
  32.             //排序  
  33.             DataView view = ub.ShowAll().DefaultView;//从datatable取出dataview用于排序  
  34.             //排序字符串   排序表达式(空格) 升序/逆序  
  35.             string sort = ViewState["SortOrder"].ToString() + " " + ViewState["SortDire"].ToString();  
  36.             //设置排序字符串  
  37.             view.Sort = sort;  
  38.             //绑定数据源  
  39.             this.GridView1.DataSource = view;  
  40.             this.GridView1.DataBind();  
  41.         }  
  42.         //下一页(用户点击页数按钮时)  
  43.         protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)  
  44.         {  
  45.             //当前页数等于 用户点击的那一页  
  46.             this.GridView1.PageIndex = e.NewPageIndex;  
  47.             //重新绑定   
  48.             Bind();  
  49.         }  
  50.         //编辑时  
  51.         protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)  
  52.         {  
  53.             //设置编辑时的行 等于用户触发事件的这个行  
  54.             this.GridView1.EditIndex = e.NewEditIndex;  
  55.             //重新绑定   
  56.             Bind();  
  57.         }  
  58.         //点击取消时(取消编辑)  
  59.         protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)  
  60.         {  
  61.             //设置当前选中行索引为-1(也就是没有)  
  62.             this.GridView1.EditIndex = -1;  
  63.             
  64.             //重新绑定   
  65.             Bind();  
  66.         }  
  67.         //编辑时的更新按钮  
  68.         protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)  
  69.         {  
  70.             //当前更新的行数  
  71.             int index = e.RowIndex;  
  72.             //(重点)获取格子里面,第index行,第0号单元格里面第0个控件(也就是TextBox)强转,获取值  
  73.             string newsTitle= ((TextBox)this.GridView1.Rows[index].Cells[0].Controls[0]).Text.ToString();  
  74.             string newsContent = ((TextBox)this.GridView1.Rows[index].Cells[1].Controls[0]).Text.ToString();  
  75.             //这里的id是绑定上去的主键  
  76.            int id = Convert.ToInt32(this.GridView1.DataKeys[index].Value) ;  
  77.             UserBLL ub = new UserBLL();  
  78.             News ne = new News();  
  79.             ne.Id = id;  
  80.             ne.Title = newsTitle;  
  81.             ne.Content = newsContent;  
  82.             if (ub.UpdateNews(ne) == 1)  
  83.             {  
  84.                 //更新成功  
  85.                 this.GridView1.EditIndex = -1;//设置编辑为否  
  86.                 this.Bind();//重新绑定  
  87.             }  
  88.             else {  
  89.                 //更新失败  
  90.                 this.GridView1.EditIndex = -1;//设置编辑为否  
  91.                 this.Bind();//重新绑定  
  92.             }  
  93.         }  
  94.         //点击删除时  
  95.         protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)  
  96.         {  
  97.             //获取产生事件的行  
  98.             int index = e.RowIndex;  
  99.             //获取主键  
  100.            int key=(intthis.GridView1.DataKeys[index].Value;  
  101.             UserBLL ub = new UserBLL();  
  102.             if (ub.delNews(key) == 1)  
  103.             {  
  104.                 //删除成功  
  105.                 this.GridView1.EditIndex = -1;//设置编辑为否  
  106.                 this.Bind();//重新绑定  
  107.             }  
  108.             else {  
  109.                 //删除失败  
  110.                 this.GridView1.EditIndex = -1;//设置编辑为否  
  111.                 this.Bind();//重新绑定  
  112.             }  
  113.         }  
  114.         //双击表列标题 排序时  
  115.         protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)  
  116.         {  
  117.             //产生点击事件的排序字符串  
  118.             string expression = e.SortExpression;  
  119.             if (ViewState["SortOrder"].ToString() == expression)  //如果上次点击的是这个,就直接设置ASC,DESC  
  120.             {  
  121.                 if (ViewState["SortDire"].ToString() == "ASC")  
  122.                 {  
  123.                     ViewState["SortDire"] = "DESC";  
  124.                 }  
  125.                 else  
  126.                 {  
  127.                     ViewState["SortDire"] = "ASC";  
  128.                 }  
  129.             }  
  130.             else {  
  131.                 //如果上次没有点击这个 就直接排序  
  132.                 ViewState["SortOrder"] = expression;  
  133.             }  
  134.             /这里只是设置一些全局变量 实际排序操作  在 DataView的Sort排序字符串里面设置  
  135.             Bind();//重新绑定  
  136.         }  
  137.     }  
  138. }  
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using Model;
using System.Data;
namespace UI
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //这里给一个初始值  (ViewState是一个字典(服务器控件的一个跨页变量))
                ViewState["SortOrder"] = "新闻标题";//排序表达式SortExpression实际就是DataField 就是T-SQL语句查询出来的列名
                //排序表达式 实际就是设置按照哪个列进行排序而已
                ViewState["SortDire"] = "ASC";//排序顺序(升序)
                Bind();
            }
        }
        //把绑定数据 写到一个方法里
        private void Bind()
        {
            UserBLL ub = new UserBLL();
           
            //(重点)设置一个标识(一般是主键)绑定在每一项上去,用于删除数据时,获取主键。(把主键id放在DataKeys)
            this.GridView1.DataKeyNames = new string[] { "id" };  //这个是绑定在行上的
            //排序
            DataView view = ub.ShowAll().DefaultView;//从datatable取出dataview用于排序
            //排序字符串   排序表达式(空格) 升序/逆序
            string sort = ViewState["SortOrder"].ToString() + " " + ViewState["SortDire"].ToString();
            //设置排序字符串
            view.Sort = sort;
            //绑定数据源
            this.GridView1.DataSource = view;
            this.GridView1.DataBind();
        }
        //下一页(用户点击页数按钮时)
        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            //当前页数等于 用户点击的那一页
            this.GridView1.PageIndex = e.NewPageIndex;
            //重新绑定 
            Bind();
        }
        //编辑时
        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            //设置编辑时的行 等于用户触发事件的这个行
            this.GridView1.EditIndex = e.NewEditIndex;
            //重新绑定 
            Bind();
        }
        //点击取消时(取消编辑)
        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            //设置当前选中行索引为-1(也就是没有)
            this.GridView1.EditIndex = -1;
          
            //重新绑定 
            Bind();
        }
        //编辑时的更新按钮
        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            //当前更新的行数
            int index = e.RowIndex;
            //(重点)获取格子里面,第index行,第0号单元格里面第0个控件(也就是TextBox)强转,获取值
            string newsTitle= ((TextBox)this.GridView1.Rows[index].Cells[0].Controls[0]).Text.ToString();
            string newsContent = ((TextBox)this.GridView1.Rows[index].Cells[1].Controls[0]).Text.ToString();
            //这里的id是绑定上去的主键
           int id = Convert.ToInt32(this.GridView1.DataKeys[index].Value) ;
            UserBLL ub = new UserBLL();
            News ne = new News();
            ne.Id = id;
            ne.Title = newsTitle;
            ne.Content = newsContent;
            if (ub.UpdateNews(ne) == 1)
            {
                //更新成功
                this.GridView1.EditIndex = -1;//设置编辑为否
                this.Bind();//重新绑定
            }
            else {
                //更新失败
                this.GridView1.EditIndex = -1;//设置编辑为否
                this.Bind();//重新绑定
            }
        }
        //点击删除时
        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            //获取产生事件的行
            int index = e.RowIndex;
            //获取主键
           int key=(int) this.GridView1.DataKeys[index].Value;
            UserBLL ub = new UserBLL();
            if (ub.delNews(key) == 1)
            {
                //删除成功
                this.GridView1.EditIndex = -1;//设置编辑为否
                this.Bind();//重新绑定
            }
            else {
                //删除失败
                this.GridView1.EditIndex = -1;//设置编辑为否
                this.Bind();//重新绑定
            }
        }
        //双击表列标题 排序时
        protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
        {
            //产生点击事件的排序字符串
            string expression = e.SortExpression;
            if (ViewState["SortOrder"].ToString() == expression)  //如果上次点击的是这个,就直接设置ASC,DESC
            {
                if (ViewState["SortDire"].ToString() == "ASC")
                {
                    ViewState["SortDire"] = "DESC";
                }
                else
                {
                    ViewState["SortDire"] = "ASC";
                }
            }
            else {
                //如果上次没有点击这个 就直接排序
                ViewState["SortOrder"] = expression;
            }
            /这里只是设置一些全局变量 实际排序操作  在 DataView的Sort排序字符串里面设置
            Bind();//重新绑定
        }
    }
}


效果:

由于时间关系,就写到这里。都写了注释。

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值