在asp.net中的网格视图中编辑更新和删除 [英] Edit update and delete from grid view in asp.net

查看:97
本文介绍了在asp.net中的网格视图中编辑更新和删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好

我正在尝试在网格视图中编辑更新和删除.我的代码如下.
ID是自动递增的.
但是现在更新和删除不起作用,请帮忙.


Hello

I am trying to edit update and delete in grid view. My code is as follows.
Id is auto incremental.
But now update and delete is not working please help.


public partial class MainTable : System.Web.UI.Page
{
    SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString);

    protected void Page_Load(object sender, EventArgs e)
    {
        //SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString);
        if (!IsPostBack)
        {
            bind();
        }
    }

    protected void GridView1_RowDeleted(object sender, GridViewDeletedEventArgs e)
    {

    }

    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        GridViewRow row=(GridViewRow)GridView1.Rows[e.RowIndex];
        Label lbldeleteid = (Label)row.FindControl("lblID");
        conn.Open();
        SqlCommand cmd = new SqlCommand("SELECT * FROM tbl_SqlImage", conn);
        cmd.ExecuteNonQuery();
        conn.Close();
        bind();



    }
    public void bind()
    {

        conn.Open();
        SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM tbl_SqlImage", conn);
        DataSet ds = new DataSet();
        da.Fill(ds, "emp");
        GridView1.DataSource = ds.Tables[0];
        GridView1.DataBind();
        conn.Close();
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        bind();
        //GridView1.DataBind();
    }

    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
        Label lblID = (Label)row.FindControl("lblID");
        TextBox textName = (TextBox)row.FindControl("txtName");
        TextBox textDesc = (TextBox)row.FindControl("txtDesc");
        TextBox textImage = (TextBox)row.FindControl("txtImage");
        TextBox textActive = (TextBox)row.FindControl("txtActive");
        TextBox textCreatedBy = (TextBox)row.FindControl("txtCreatedBy");
        TextBox textCreatedDate = (TextBox)row.FindControl("txtCreatedDate");
        GridView1.EditIndex = -1;
        conn.Open();
        SqlCommand cmd = new SqlCommand("SELECT * FROM tbl_SqlImage", conn);
        cmd.ExecuteNonQuery();
        conn.Close();
        bind();
        //GridView1.DataBind();

    }



    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        bind();

    }

    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
         GridView1.EditIndex = -1;
         bind();
    }



}



请帮助解决代码的问题.
请帮忙.



Please help what''s wrong with the code.
Please help.

推荐答案

使用此方法:
use this :
 protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            GridViewRow row=(GridViewRow)GridView1.Rows[e.RowIndex];
            Label lbldeleteid = (Label)row.FindControl("lblID");
            conn.Open();
            SqlCommand cmd = new SqlCommand("delete * FROM tbl_SqlImage where id_field=''"+lbldeleteid+"''", conn);
            cmd.ExecuteNonQuery();
            conn.Close();
            bind();
 

 
        }



 protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
            Label lblID = (Label)row.FindControl("lblID");
            TextBox textName = (TextBox)row.FindControl("txtName");
            TextBox textDesc = (TextBox)row.FindControl("txtDesc");
            TextBox textImage = (TextBox)row.FindControl("txtImage");
            TextBox textActive = (TextBox)row.FindControl("txtActive");
            TextBox textCreatedBy = (TextBox)row.FindControl("txtCreatedBy");
            TextBox textCreatedDate = (TextBox)row.FindControl("txtCreatedDate");
            GridView1.EditIndex = -1;
            conn.Open();
            SqlCommand cmd = new SqlCommand("update tablename set name=''"+textname.Text+"'',desc=''"+txtdesc.Text+"'' where id=''"+lblID+"'', conn); //  Here set Update Query to update the Field.
            cmd.ExecuteNonQuery();
            conn.Close();
            bind();
            //GridView1.DataBind();
            
        }


this will help you..........


在命令写入更新和删除查询中将更新数据.您只需编写选择查询即可.

in command write update and delete query which will update the data. u have simply write select query.

protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            GridViewRow row=(GridViewRow)GridView1.Rows[e.RowIndex];
            Label lbldeleteid = (Label)row.FindControl("lblID");
            conn.Open();
            SqlCommand cmd = new SqlCommand("delete FROM tbl_SqlImage where id="+ lbldeleteid.Text, conn);
            cmd.ExecuteNonQuery();
            conn.Close();
            bind();
 
        }


在更新中写


in update write

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {


SqlCommand cmd = new SqlCommand("update  tbl_SqlImage set Name='"+ textName.text +"' where id="+ lbldeleteid.Text, conn);
}



有关更多信息,请参见此文章 [



for more look in this article[^]



浏览此链接-
gridview-sqldatasource-insert-edit-delete.html [ ^ ]
Hi,
Go through this link-
gridview-sqldatasource-insert-edit-delete.html[^]


这篇关于在asp.net中的网格视图中编辑更新和删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆