• GridView 增删改 狼


    要点:1.不能是自动列,必须是手动列。;2.后台必须绑定主键,否则在进行操作时获取不到主键  GridView1.DataKeyNames = new string[] { "userid" };//主键
    3.    展示列用,可以实现编辑<asp:BoundField DataField="username" HeaderText="用户姓名" />,编辑列 <asp:CommandField HeaderText="编辑" ShowEditButton="true" />;
    4.后台查找控件值,使用方式(  string up = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim();表示在当前行的第1+1列,的第1(顺序是0)个控件,的值)。
    5.编辑列事件必须实现  GridView1.EditIndex = e.NewEditIndex;   Bindgd()(数据绑定);;
    6.他们的所有事件都是必须是以ing结尾的(如编辑事件 OnRowEditing="GridView1_RowEditing")。
    7.获取主键id:  string s = GridView1.DataKeys[e.RowIndex].Value.ToString();表示获得当前行的主键值。
    8.DataField:表示当前字段的值,可以是一个数组;DataFormatString,表示格式化当前指端显示在出来的格式,他用 XXXX{0}XXXX{1},的格式方式,其中{0},表示要寻找DataField中的第一个值,{1}表示寻找DataField中的第二个值。
    快速预览:
    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

    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;
    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 _Default : System.Web.UI.Page
    {

    //清清月儿http://blog.csdn.net/21aspnet  
      SqlConnection sqlcon;
      SqlCommand sqlcom;
      string strCon = "Data Source=(local);Database=数据库名;Uid=帐号;Pwd=密码";
      protected void Page_Load(object sender, EventArgs e)
      {
      if (!IsPostBack)
      {
      bind();
      }
      }
      protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
      {
      GridView1.EditIndex = e.NewEditIndex;
      bind();
      }

    //删除
      protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
      {
      string sqlstr = "delete from 表 where id='" + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
      sqlcon = new SqlConnection(strCon);
      sqlcom = new SqlCommand(sqlstr,sqlcon);
      sqlcon.Open();
      sqlcom.ExecuteNonQuery();
      sqlcon.Close();
      bind();
      }

    //更新
      protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
      {
      sqlcon = new SqlConnection(strCon);
      string sqlstr = "update 表 set 字段1='"
      + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim() + "',字段2='"
      + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim() + "',字段3='"
      + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim() + "' where id='"
      + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
      sqlcom=new SqlCommand(sqlstr,sqlcon);
      sqlcon.Open();
      sqlcom.ExecuteNonQuery();
      sqlcon.Close();
      GridView1.EditIndex = -1;
      bind();
      }

    //取消
      protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
      {
      GridView1.EditIndex = -1;
      bind();
      }

    //绑定
      public void bind()
      {
      string sqlstr = "select * from 表";
      sqlcon = new SqlConnection(strCon);
      SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);
      DataSet myds = new DataSet();
      sqlcon.Open();
      myda.Fill(myds, "表");
      GridView1.DataSource = myds;
      GridView1.DataKeyNames = new string[] { "id" };//主键
      GridView1.DataBind();
      sqlcon.Close();
      }
    }

    前台主要代码:
      ... ...
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4"
      ForeColor="#333333" GridLines="None" OnRowDeleting="GridView1_RowDeleting" OnRowEditing="GridView1_RowEditing"
      OnRowUpdating="GridView1_RowUpdating" OnRowCancelingEdit="GridView1_RowCancelingEdit">
      <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
      <Columns>
      <asp:BoundField DataField="身份证号码" HeaderText="用户ID" ReadOnly="True" />
      <asp:BoundField DataField="姓名" HeaderText="用户姓名" />
      <asp:BoundField DataField="员工性别" HeaderText="性别" />
      <asp:BoundField DataField="家庭住址" HeaderText="家庭住址" />
      <asp:CommandField HeaderText="选择" ShowSelectButton="True" />
      <asp:CommandField HeaderText="编辑" ShowEditButton="True" />
      <asp:CommandField HeaderText="删除" ShowDeleteButton="True" />
      </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>


    3.GridView正反双向排序:
    效果图:点姓名各2次的排序,点其他也一样可以。

    后台代码:
    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;
    using System.Data.SqlClient;
    public partial class Default3 : System.Web.UI.Page
    {

    //清清月儿的博客http://blog.csdn.net/21aspnet  
      SqlConnection sqlcon;
      string strCon = "Data Source=(local);Database=北风贸易;Uid=sa;Pwd=";
      protected void Page_Load(object sender, EventArgs e)
      {
      if (!IsPostBack)
      {
      ViewState["SortOrder"] = "身份证号码";
      ViewState["OrderDire"] = "ASC";
      bind();
      }
      }
      protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
      {
      string sPage = e.SortExpression;
      if (ViewState["SortOrder"].ToString() == sPage)
      {
      if (ViewState["OrderDire"].ToString() == "Desc")
      ViewState["OrderDire"] = "ASC";
      else
      ViewState["OrderDire"] = "Desc";
      }
      else
      {
      ViewState["SortOrder"] = e.SortExpression;
      }
      bind();
      }

      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, "飞狐工作室");
      DataView view = myds.Tables["飞狐工作室"].DefaultView;
      string sort = (string)ViewState["SortOrder"] + " " + (string)ViewState["OrderDire"];
      view.Sort = sort;
      GridView1.DataSource = view;
      GridView1.DataBind();
      sqlcon.Close();
      }
    }

    前台主要代码:
    <asp:GridView ID="GridView1" runat="server" AllowSorting="True" AutoGenerateColumns="False"
      CellPadding="3" Font-Size="9pt" OnSorting="GridView1_Sorting" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px">
      <FooterStyle BackColor="White" ForeColor="#000066" />
      <Columns>
      <asp:BoundField DataField="身份证号码" HeaderText="用户ID" SortExpression="身份证号码" />
      <asp:BoundField DataField="姓名" HeaderText="用户姓名" 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>
  • 相关阅读:
    Eclipse无法启动报An internal error occurred during: "reload maven project". java.lang.NullPointerExceptio错
    axios拦截器
    antd 之TimePicker设置禁用时间
    使用dva和ant-design组件在IE中的兼容问题记录
    Electron对JQuery的支持
    将本地项目上传到github时,问题总结
    ‘时间等待’加载处理
    6、模块和包的导入
    1、python的安装
    3、简单小程序练习
  • 原文地址:https://www.cnblogs.com/gowhy/p/2020495.html
Copyright © 2020-2023  润新知