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

1.界面

<asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333"


            GridLines="None" AutoGenerateColumns="False"  DataKeyNames="ProductID"

            onrowdatabound="GridView1_RowDataBound" AllowPaging="True"

            onpageindexchanging="GridView1_PageIndexChanging"

            onrowcommand="GridView1_RowCommand"

            onrowcancelingedit="GridView1_RowCancelingEdit"

            onrowediting="GridView1_RowEditing" onrowupdating="GridView1_RowUpdating"


            onrowdeleting="GridView1_RowDeleting">

            <PagerSettings FirstPageText="首页" LastPageText="尾页"

                Mode="NextPreviousFirstLast" NextPageText="下一页" PreviousPageText="上一页" />

            <RowStyle BackColor="#E3EAEB" />

            <Columns>

                <asp:TemplateField HeaderText="ProductID">

 

                    <ItemTemplate>

                        <asp:Label ID="Label2" runat="server" Text='<%# Bind("ProductID") %>'></asp:Label>

                    </ItemTemplate>

                </asp:TemplateField>

                <asp:TemplateField HeaderText="ProductName">

                    <EditItemTemplate>

                        <asp:TextBox ID="txtName" runat="server" Text='<%# Bind("ProductName") %>'></asp:TextBox>

                    </EditItemTemplate>

                    <ItemTemplate>

                        <asp:Label ID="Label1" runat="server" Text='<%# Bind("ProductName") %>'></asp:Label>

                    </ItemTemplate>

                </asp:TemplateField>

                <asp:TemplateField HeaderText="UnitPrice">

                    <ItemTemplate>

                        <asp:Label ID="Label3" runat="server" Text='<%# Bind("UnitPrice") %>'></asp:Label>

                    </ItemTemplate>

                </asp:TemplateField>

                <asp:TemplateField HeaderText="操">

<ItemTemplate>

                        <asp:LinkButton ID="del" runat="server" OnClientClick="return confirm('您确定要删除吗?')" CommandName="dell" >删除</asp:LinkButton>

                      

                    </ItemTemplate>

                </asp:TemplateField>

                <asp:CommandField ShowEditButton="True" HeaderText="作">

               

                    <HeaderStyle  HorizontalAlign="Center" />

                        <ItemStyle  HorizontalAlign="Center"  Width="85px" ForeColor="Blue" />

                    </asp:CommandField>

            </Columns>

            <FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White"

                HorizontalAlign="Right" />

            <PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />

            <SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />

            <HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />

            <EditRowStyle BackColor="#7C6F57" />

            <AlternatingRowStyle BackColor="White" />

        </asp:GridView>

2.前台操控调用业务

DalBll db = new DalBll();

    static List<Products> tmpList = new List<Products>();

    protected void Page_Load(object sender, EventArgs e)

    {

       

        if(!IsPostBack)

        {

            InitGridView();

        }

    }

private void InitGridView()

    {

        tmpList = db.GetDataList();

        this.GridView1.DataSource = tmpList;

        this.GridView1.DataBind();

    }

//绑定数据时触发

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)

    {

        if (e.Row.RowType == DataControlRowType.DataRow)

        {

            Products tmp = e.Row.DataItem as Products;

            LinkButton lbtn = e.Row.FindControl("del") as LinkButton;

            if (lbtn != null && tmp != null)

                lbtn.CommandArgument = tmp.ProductID.ToString();//绑定主键

        }

    }

//删除数据

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)

    {

        if (e.CommandName == "dell")

        {

            int productID = Convert.ToInt32(e.CommandArgument);

            db.Del(productID);

        }

}

//分页

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)

    {

        this.GridView1.PageIndex = e.NewPageIndex;

        InitGridView();

    }

//切换到编辑模式

    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)

    {

        this.GridView1.EditIndex = e.NewEditIndex;

        InitGridView();

    }

//取消

    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)

    {

        this.GridView1.EditIndex = -1;

        InitGridView();

    }

    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)

    {

        //获取当前页索引

        int i = this.GridView1.EditIndex;

        //获取文本框的值

        string productsName = ((TextBox)(this.GridView1.Rows[i].FindControl("txtName"))).Text.ToString();

        DataKey key = this.GridView1.DataKeys[e.RowIndex];

        int id = Convert.ToInt32(key[0].ToString());

        //首先找到该对象

        Products tmp = tmpList.Where(c => c.ProductID == id).First();

        tmp.ProductName = productsName;

        db.Update(tmp);

        InitGridView();

    }

3.各操作数据的代码

public class DalBll

    {

        NorthwindEntities db = new NorthwindEntities();

public List<Products> GetDataList()

        {

            return db.Products.ToList();

        }

public void Del(int productID)

        {

            Products tmp = db.Products.Where(c=>c.ProductID==productID).First();

            db.DeleteObject(tmp);

            db.SaveChanges();

        }

public void Update(Products tmp)

        {

            //为參数对象创建实体键

            EntityKey key;

            object originalProductObj;

            //因參数对象不属于上下文,因此为该參数对象创建上下文实体键

            key = db.CreateEntityKey("Products", tmp);

            db.TryGetObjectByKey(key, out originalProductObj);

            db.ApplyPropertyChanges(key.EntitySetName, tmp);

            db.SaveChanges();

           

        }

GridView行编辑、更新、取消、删除事件使用方法的更多相关文章

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

    gridview编辑列,把左下角的"自动生成字段"的复选框的勾去掉 添加boundfield(绑定列)将其datafield设置为productname,headertext设置为 ...

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

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

  3. php+mysqli实现批量执行插入、更新及删除数据的方法

    本文实例讲述了php+mysqli实现批量执行插入.更新及删除数据的方法.分享给大家供大家参考.具体如下: mysqli批量执行插入/更新/删除数据,函数为 multi_query(). 下面的代码只 ...

  4. JS框架avalon简单例子 行编辑 添加 修改 删除 验证

    为什么要写这个例子:做表单的时候,表单包含主子表,对于子表的编辑,使用的是easyui datagrid的行编辑功能,由于业务比较复杂,实现起来比较麻烦,代码写的也很多,因为插件的封装,无法操作原始的 ...

  5. arcgis engine 监听element的添加、更新和删除事件(使用IGraphicsContainerEvents)

    IGraphicsContainerEvents Interface 如何监听 element事件? 如,当我们在Mapcontrol上添加.删除.更新了一个Element后,如何捕捉到这个事件?   ...

  6. arcgis engine 监听element的添加、更新和删除事件(使用IMovePointFeedback)

    VB代码: 复制进程序稍作修改变量名和事件逻辑即可使用. Members   AllPropertiesMethodsInheritedNon-inherited Description Displa ...

  7. jqGrid行编辑配置,方法,事件

    行编辑可以在行修改后更新数据,如下图所示 用户用鼠标点击选择一行,jqGrid将可编辑的字段转换为数据输入单元,如上面图所示.不可编辑的列,如id,不会转为可输入单元,而是保持不变.可以通过配置col ...

  8. jqGrid单元格编辑配置,事件及方法

    转自 http://blog.csdn.net/xueshijun666/article/details/18151055 // var ret = $("#in_store_list_de ...

  9. Python编程之列表操作实例详解【创建、使用、更新、删除】

    Python编程之列表操作实例详解[创建.使用.更新.删除] 这篇文章主要介绍了Python编程之列表操作,结合实例形式分析了Python列表的创建.使用.更新.删除等实现方法与相关操作技巧,需要的朋 ...

随机推荐

  1. php页面相互调用的知识点

    目前我们有这样一个需求: (1) a.php  页面要使用 b.php 定义的函数,我们可以使用 如下指令 require  require_once include   include_once 举 ...

  2. hdu 5724 Chess 博弈

    题目链接 一个n行20列的棋盘. 每一行有若干个棋子. 两人轮流操作, 每人每次可以将一个棋子向右移动一个位置, 如果它右边有一个棋子, 就跳过这个棋子, 如果有若干个棋子, 就将这若干个都跳过. 但 ...

  3. vagrant打造自己的开发环境

    vagrant打造自己的开发环境 缘由: 在网上看到斌哥,爽神都写了关于vagrant的博客,都在说很强大,所以很好奇这玩意怎么个强大,然后也就自己来一发玩玩看看. 真实缘由: 说实话是电脑配置太低, ...

  4. 提示text还能输入多少字节

    1.添加jQuery自定义扩展 $(function($){ // tipWrap: 提示消息的容器 // maxNumber: 最大输入字符 $.fn.artTxtCount = function( ...

  5. new Date().getTime()

    new Date().getTime()获取当前时间戳; 不知道是毫秒级还是千分之一毫秒级别

  6. iOS 退出应用程序

    退出应用程序,方法很简单,只是动画效果没有那么好. - (void)exitApplication { AppDelegate *app = [UIApplication sharedApplicat ...

  7. Labeling Balls(拓扑排序wa)

    Labeling Balls Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12466   Accepted: 3576 D ...

  8. Android图片裁剪之自由裁剪

    我的博客http://blog.csdn.net/dawn_moon 客户的需求都是非常怪的.我有时候在给客户做项目的时候就想骂客户是sb.可是请你相信我,等你有需求,自己变成客户的时候,给你做项目的 ...

  9. CodeForces Round #179 (295A) - Greg and Array 一个线段树做两次用

    线段树的区间更新与区间求和...一颗这样的线段树用两次... 先扫描1~k...用线段树统计出每个操作执行的次数... 那么每个操作就变成了 op. l  , op.r , op.c= times* ...

  10. web - 清除浮动

    最理想的方式为 伪类 + content : 例如 div:after{content:"";display:block;clear:both;} div{zoom:1;} 另外, ...