GridView 添加,修改,删除

 GridView 添加,修改,删除事件前后台代码


GridView 控件代码   前台代码

        <asp:GridView ID="GridView1" runat="server" AllowPaging="True"
            DataKeyNames="id" onpageindexchanging="GridView1_PageIndexChanging"
            onrowcancelingedit="GridView1_RowCancelingEdit"
            onrowdeleting="GridView1_RowDeleting" onrowediting="GridView1_RowEditing"
            onrowupdating="GridView1_RowUpdating" PageSize="5">
            <Columns>
                <asp:CommandField ShowEditButton="True" />
                <asp:CommandField ShowDeleteButton="True" />
            </Columns>
        </asp:GridView>


CS文件代码

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack) {
            baindata();
        }
    }

    //正在删除的时候激发
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        //获取要删除的记录的id并赋值给变量ID
        string ID = this.GridView1.DataKeys[e.RowIndex][0].ToString();
        logic.Class1 abc = new logic.Class1();
        string Sql = "DELETE FROM " + configure.Class1.Dataname + " where id=" + ID + "";
        abc.Exesql(Sql);
        baindata();
    }

    //换页后触发
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        //更新页值
        this.GridView1.PageIndex = e.NewPageIndex;
        baindata();//重新绑定记录
    }

    //点击编辑后触发
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        this.GridView1.EditIndex = e.NewEditIndex;
        baindata();
    }

    //当取消时触发
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        this.GridView1.EditIndex = -1;
        baindata();
    }

    //当更新时触发
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        //获取要编辑的记录的id并赋值给变量ID
        string ID = this.GridView1.DataKeys[e.RowIndex][0].ToString();
        string text1 = ((TextBox)this.GridView1.Rows[e.RowIndex].Cells[3].Controls[0]).Text.ToString();
        string text2 = ((TextBox)this.GridView1.Rows[e.RowIndex].Cells[4].Controls[0]).Text.ToString();
        string text3 = ((TextBox)this.GridView1.Rows[e.RowIndex].Cells[5].Controls[0]).Text.ToString();
        string text4 = ((TextBox)this.GridView1.Rows[e.RowIndex].Cells[6].Controls[0]).Text.ToString();
        string text5 = ((TextBox)this.GridView1.Rows[e.RowIndex].Cells[7].Controls[0]).Text.ToString();
        logic.Class1 abc = new logic.Class1();
        abc.Exesql("update book set title='" + text1 + "',content='" + text2 + "',addman='" + text3 + "',ip='" + text4 + "',addtime='" + text5 + "' where id=" + ID + "");
        this.GridView1.EditIndex = -1;
        baindata();
    }

    //绑定数据库
    public void baindata()
    {
        //实例化命名空间logic中的class1类
        logic.Class1 abc = new logic.Class1();
        //获取abc返回的DataSet
        DataSet da = abc.Opendata(configure.Class1.Sqlexe);
        //指定数据源
        this.GridView1.DataSource = da.Tables[0].DefaultView;//GridView1的数据源=DataSet返回数据
        //将数据绑定到GridView1
        this.GridView1.DataBind();
    }
}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值