GridView 的添加,删除,更新 操作

protected void GV_yaoqiu_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GV_yaoqiu.EditIndex = -1;
        yaoqiu_databind();
    }
    protected void GV_yaoqiu_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string id = GV_yaoqiu.DataKeys[e.RowIndex].Values[0].ToString();
        string sql = "delete from T_Require where PK_Require=" + id;
        try
        {
            int i = DbHelperSQL.ExecuteSql(sql);
            GV_yaoqiu.EditIndex = -1;
            yaoqiu_databind();
        }
        catch (Exception ex)
        {
            Response.Write("<script>alert('" + ex.Message.ToString() + "');</script>");
        }
    }
    protected void GV_yaoqiu_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GV_yaoqiu.EditIndex = e.NewEditIndex;
        yaoqiu_databind();
    }
    protected void GV_yaoqiu_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        string id = GV_yaoqiu.DataKeys[e.RowIndex].Values[0].ToString();

        string require_name = ((TextBox)GV_yaoqiu.Rows[e.RowIndex].FindControl("tb_yaoqiu")).Text.Trim();

        string sqltext = "update T_Require set require_name='" + require_name + "' where PK_Require=" + id;

        try
        {
            int i = DbHelperSQL.ExecuteSql(sqltext);
            GV_yaoqiu.EditIndex = -1;
            yaoqiu_databind();
        }
        catch (Exception ex)
        {
            Response.Write("<script>alert('" + ex.Message.ToString() + "');</script>");
        }
    }
    protected void GV_yaoqiu_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)           //添加删除询问
        {
            e.Row.Cells[3].Attributes.Add("onclick", "javascript:return confirm('你确认要删除该记录吗?')");

        }
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        try
        {
            string newrequire = ((TextBox)this.FindControl("new_yaoqiu")).Text.Trim();
            string sql = "insert into T_Require(require_name) values('" + newrequire + "') ";

            DbHelperSQL.ExecuteSql(sql);
            yaoqiu_databind();
            ((TextBox)this.FindControl("new_yaoqiu")).Text = string.Empty;
        }
        catch (Exception ex)
        {
            Response.Write("<script>alert('" + ex.Message.ToString() + "');</script>");
        }
    }
}

private void yaoqiu_databind()                    //数据绑定函数
    {
        string sql = "select PK_Require ,Require_name from T_Require order by PK_Require";
        try
        {
            GV_yaoqiu.DataSource = DbHelperSQL.Query(sql).Tables[0].DefaultView;
            GV_yaoqiu.DataBind();
        }
        catch (Exception ex)
        {
            Response.Write("<script>alert('" + ex.Message.ToString() + "')</script>");

        }
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值