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. hadoop+zookeeper(ha架构搭建)

    http://blog.csdn.net/baidu_25820069/article/details/52225293 [条件所限,待验证]

  2. urllib 和urllib2 模块使用简例

    一.最简单的使用 import urllib,urllib2 response = urllib2.urlopen("https://www.baidu.com") print r ...

  3. MVC 发布到IIS中的配置方法

    MVC 发布到IIS中的配置方法  http://msdn.microsoft.com/zh-cn/library/gg703322(v=vs.98).aspx

  4. smartos介绍

    https://wiki.smartos.org A Little History 2005年,Sun Microsystems开源了其著名的Unix操作系统Solaris,最终被发布为一个名为Ope ...

  5. java script btoa与atob的

    javascript原生的api本来就支持,Base64,但是由于之前的javascript局限性,导致Base64基本中看不中用.当前html5标准正式化之际,Base64将有较大的转型空间,对于H ...

  6. EasyUI 列表展示及基本格式

    <html> <head> <meta http-equiv="Content-Type" content="text/html; char ...

  7. python学习——sys.argv

    sys.argv[]:用于获取命令行参数,sys.argv[0]即所运行的代码自身的文件路径,因此真正的其他参数是从1开始 sys.argv[1]:表示第一个参数 sys.argv[1][2:]:表示 ...

  8. 算法笔记_067:蓝桥杯练习 算法训练 安慰奶牛(Java)

    目录 1 问题描述 2 解决方案   1 问题描述 问题描述 Farmer John变得非常懒,他不想再继续维护供奶牛之间供通行的道路.道路被用来连接N个牧场,牧场被连续地编号为1到N.每一个牧场都是 ...

  9. c语言指针数组和结构体的指针

    指向数组的指针,先初始化一个数组,使用传统方式遍历 void main() { ] = { ,,,, }; ; i < ; i++) { printf("%d,%x\n", ...

  10. sublime 配置python环境

    1. 在工具栏点击Preferences,打开Browse Packages.在打开的文件夹中找到Python,并打开这个文件夹.找到文件Python.sublime-build,并打开. 2. 修改 ...