• gridview的编辑,更新,取消,自动分页等


    gridview编辑列,把左下角的"自动生成字段"的复选框的勾去掉

    添加boundfield(绑定列)将其datafield设置为productname,headertext设置为"产品名"

    再添加commadfield下的编辑,更新,取消

    确定

    .cs文件按中编写一绑定gridview数据的方法:

    public void bind()
        {
            SqlConnection conn = new SqlConnection("server=.;uid=sa;pwd=;database=Northwind");
            conn.Open();
            SqlDataAdapter sda = new SqlDataAdapter("select * from products", conn);
            DataSet ds = new DataSet();
            sda.Fill(ds);
            conn.Close();
            this.GridView1.DataSource = ds;
            this.GridView1.DataBind();
        }

    在load事件中调用bind()方法.......

    将gridview的datakeynames设置为productid(和datalist的datakeyfield有点像)

    1.编辑:

    找到gridview的RowEditing事件:

    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex; //编辑的行设置为鼠标作用的当前行
            bind(); //一定要进行绑定
        }

    2.更新:找到gridview的RowUpdating事件:

    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)

    {

       {
            int i = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value); //关键代码:取当前选中行产品的id
            string productname = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[0].Controls[0])).Text; //关键代码:取编辑框中产品名(改过后的名称),Rows[e.RowIndex].Cells[0].Controls[0]表示当前行,第一列(cell[0])第一个控件(control[0])
            SqlConnection conn = new SqlConnection("server=.;uid=sa;pwd=;database=Northwind");
            conn.Open();
            string strSQL = "update products set productname='" + productname + "'where productid='" + i + "'";
            SqlCommand cmd = new SqlCommand(strSQL, conn);
           int h=cmd.ExecuteNonQuery();
           if (h > 0)
           {
               Response.Write("<script>alert('success')</script>");
               GridView1.EditIndex = -1; //一定要设置,否则还处于编辑状态
           }
             
           else
               Response.Write("<script>alert('success')</script>");
            bind();
        }

    }

    3.取消:找到gridview的RowCancelingEdit事件:

    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridView1.EditIndex = -1;
            bind(); //最后还要重新绑定,否则还是呈现"更新"和"取消"
        }

    4.自动分页:

    将gridview的allowpaging设置为true,pagesize自己定义如5

    找到gridview的PageIndexChanging事件:

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            bind(); //一定要重新绑定,否则是没效果的
        }

    5.gridview的rowcommand事件中取主键://模板列中放一button并把他的commandname设置为show

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "show")
            {
                //GridViewRow drv=((GridViewRow)(((Button)(e.CommandSource)).Parent.Parent));                    
                //string index = drv.RowIndex.ToString(); //行号
                //string admin_id = GridView1.DataKeys[Convert.ToInt32(index)].Value.ToString(); //主键
                //Response.Write(index+";"+admin_id);

    //下面的方法需要在前台把button的    CommandArgument ='<%# Eval("admin_id")%>'       

                int admin_id=Convert.ToInt32(e.CommandArgument.ToString()); //主键
                int index = Convert.ToInt32(((GridViewRow)(((Button)(e.CommandSource)).Parent.Parent)).RowIndex); //行号
                Response.Write(admin_id+";"+index);


            }
        }

  • 相关阅读:
    《jmeter:菜鸟入门到进阶系列》
    Jmeter下载时Binaries和Source两类包的区别
    MySQL5.7 四种日志文件
    Windows下配置nginx+php(wnmp)
    回望2018,计划2019
    C# 单元测试(入门)
    C# 中out,ref,params参数的使用
    C# 程序运行中的流程控制
    Nacos(五):多环境下如何“读取”Nacos中相应的配置
    Nacos(四):SpringCloud项目中接入Nacos作为配置中心
  • 原文地址:https://www.cnblogs.com/gaoshuai/p/3221185.html
Copyright © 2020-2023  润新知