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

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

大家好,又见面了,我是全栈君,祝每个程序员都可以多学几门语言。

 

注意:当启用编辑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();
           
        }

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/118540.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 安装PyTorch(pytorch官网下载教程)

    安装PyTorch过程安装anaconda环境管理PyTorch安装检验安装安装anaconda登录anaconda的官网下载,anaconda是一个集成的工具软件不需要我们再次下载。anaconda官网点击下载跳转到这个页面如果你的Python版本正好是3.8版,那便可以直接根据系统去选择自己相应的下载版本就可以了。但是如果你的Python版本号不是当前页面的版本号,那我建议你去选择相对应的版本号。点击archive你就会跳转到下面的页面你可以访问这篇博客去找到当前与你python版本号相对

    2022年4月15日
    52
  • android之layout_toLeftOf和layout_toRightOf出现的错误

    错误是这样的,布局文件如下,总是在SeekBar里面的这一句android:layout_toLeftOf=”@id/voice_max”报错,差点气得我吐血,明明没有错误的. 

    2022年3月9日
    46
  • GDI绘制_matlab中intlinprog函数用法

    GDI绘制_matlab中intlinprog函数用法设备上下文绘图有很多种方法。例如通过创建位图画刷,利用其填充一个区域来实现图像的绘制。此外,还可以使用CDC类的位图函数来输出位图到设备上下文中。BitBlt用于从原设备中复制位图到目标设备,语法格式如下:BOOLBitBlt(intx,inty,intnWidth,intnHeight,CDC*pSrcDC,intxSrc,intySrc,DWORDdwRop);x:目

    2022年10月18日
    3
  • Maven 入门教程

    Maven 入门教程   maven是一个项目管理工具,不仅可以把源代码构建为可以发布的项目(包括编译、打包、测试、分发),还可以生成报告、生成web站点。本文介绍maven的主要使用过程,作为入门1、maven的安装   windows下载地址http://maven.apache.org/download.cgi zip解压缩包,无需安装直接解压即可。   配置环境变量:MAVE…

    2025年10月6日
    6
  • android之通过Button的监听器往adapter中添加数据时出错

    本来源代码如下: List model; //自定义的一个List数据,存储的是自定义的类 LunchListAdapter adapter;//自定义的一个ListView的适配器 ……//省略class onSavaLis implements OnClickListener{ //Button s

    2022年3月10日
    41
  • linux 下查看有当前文件夹有多少个文件

    linux 下查看有当前文件夹有多少个文件

    2021年10月15日
    55

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号