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);

}
    }

gridview的编辑,更新,取消,自动分页等的更多相关文章

  1. GridView编辑、取消按钮自定义控件

    这个需求来自于论坛一位坛友提出的问题,他希望能够自定义编辑.取消按钮,而不是用GridView自带的编辑和取消.这里只当抛砖引玉,提出一些解决方案. 首先在页面前台设置一个GridView. < ...

  2. 单击GridView进入编辑模式

    一直以来,Insus.NET在实现GridView编辑时,均是在每笔记录第一列或是最后一列放置编辑铵钮,点击编辑铵钮之后,进行编辑模式.本博文是使用另外方式,即是点击GridView记录行任一位置,进 ...

  3. AngularJS进阶(十一)AngularJS实现表格数据的编辑,更新和删除

    AngularJS实现表格数据的编辑,更新和删除 效果 实现 首先,我们先建立一些数据,当然你可以从你任何地方读出你的数据 var app = angular.module('plunker', [' ...

  4. JasperReports 取消自动分页/忽略分页

    因为需要将合同比价单由PDF文档形式改为HTML页面方式,虽然转换文档类型了,但是发现HTML页面中间到了一定行数就出现了空行把Detail给隔开了.之前总想着怎样消除中间空行,以为是报表top页面边 ...

  5. 【ADO.NET基础-GridView】GridView的编辑、更新、取消、删除以及相关基础操作代码

    代码都是基础操作,后续功能还会更新,如有问题欢迎提出和提问....... 前台代码: <asp:GridView ID=" OnRowDataBound="GridView1 ...

  6. ASP.NET(C#) GridView (编辑、删除、更新、取消)

    转自:http://my.oschina.net/dldlhrmc/blog/93458 前台代码 view source   print? 01 <%@ Page Language=" ...

  7. GridView行编辑、更新、取消、删除事件使用方法

    注意:当启用编辑button时,点击编辑button后会使一整行都切换成文本框.为了是一行中的一部分是文本框,须要把以整行的全部列都转换成模板,然后删掉编辑模板中的代码.这样就能使你想编辑的列转换成文 ...

  8. 使用jQuery实现一个类似GridView的编辑,更新,取消和删除的功能

    先来看看下面实时效果演示: 用户点击编辑时,在点击行下动态产生一行.编辑铵钮变为disabled.新产生的一行有更新和取消的铵钮,点击“取消”铵钮,删除刚刚动态产生的行.编辑铵钮状态恢复. 更新与删除 ...

  9. ASP.NET中的GridView自带的编辑更新功能

    string ConStr = ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].Connec ...

随机推荐

  1. Kotlin语言学习笔记(6)

    运算符重载(Operator overloading) 一元运算符 Expression Translated to +a a.unaryPlus() -a a.unaryMinus() !a a.n ...

  2. Delphi笔记-自定义提示窗口

    unit pbHint; interface uses Windows, Controls, Forms, Graphics; type TPBHint=class(THintWindow) //要自 ...

  3. cat 生成文件 运行脚本

    nohup python -u day_std_cid_list_data_done.py >eee1.log 2>&1 & 后台运行python脚本 hadoop fs ...

  4. Python在pycharm中编程时应该注意的问题汇总

    1.缩进问题 在 pycharm 中点击 enter 自动进行了换行缩进,此时应该注意:比如 if   else  语句,后面跟着打印输出 print 的时候,一定注意是要if语句下的输出还是else ...

  5. 【Java杂记】Equals 和 hashCode

    equals 和 hashCode含义 equal:判断两个对象是否相等,如果相同,返回true 否则返回false hashcode: 返回一个int数 Object 默认(内部地址转化为一个数字) ...

  6. Linux下php5.3.3安装mcrypt扩展

    具体操作: 一.下载软件包 1.下载php(版本要与系统安装的一致) http://pan.baidu.com/s/1mifTbfE 2.下载libmcrypt(安装mcrypt需要此软件包) htt ...

  7. easyui input未设id导致的问题

    今天又踩了一个坑,大致是没有给input设id,使用类选择器绑定easyui控件,然后使用name设值,现在值设进去后界面没有显示. 做的界面部分截图如图: 点击下面两个橙色的按钮,通过调用下面的方法 ...

  8. 根据数组下标在MongoDB中修改数组元素

    如下图这样的数据: 即文档中某个字段是一个数组,而每个数组元素又是一个对象,现在需求是对每个对象中的content字段值作情感分析后,把情感分析得到的结果增加到这个对象中去. 如上图中第1个元素,修改 ...

  9. 第六章 图(c)广度优先搜索

  10. 56. Merge Intervals (Array; Sort)

    Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,1 ...