DataGridView添加,修改,删除事件后台代码(转载)

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

posted @ 2009-06-30 16:04  tony smith  阅读(711)  评论(0编辑  收藏  举报