帮助删除gridview? [英] help in delete in gridview ?

查看:87
本文介绍了帮助删除gridview?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好

首先,我想从架构中获取表并选择表以在另一页的gridview中显示其数据
我想添加删除&将模板编辑到此网格视图,但是我有错误
请帮帮我吗?


获取我使用的架构表:

数据源中的Sql语句:

SELECT TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME, TABLE_TYPE FROM INFORMATION_SCHEMA.TABLES



protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {

             string y  = GridView1.Rows[GridView1.SelectedIndex].Cells[1].Text;
             Session["table_name"] = y;
             Response.Redirect("Default2.aspx");
    }



在下一页获取我使用的表格数据:

protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {

            table_columns();

        }

    }
private void table_columns()
    {
        string x;
        x= Session["table_name"].ToString();
        string sql = "Select * from " + x+ "";
        SqlCommand cmd = new SqlCommand(sql, connstr);

        SqlDataAdapter dt = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        dt.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();


        connstr.Close();
    }

解决方案

You must set the DataKeyNames property in order for the automatic update and delete features of the GridView control to work.values of these key fields are passed to the data source control in order to specify the row to update or delete.



所以先设置datakeyname ..


通过此链接进入
http://www.codedigest.com/Articles/ASPNET/212_How_to_edit_and_Delete_records_in_a_gridview> br/>

添加删除按钮作为模板字段

< asp:templatefield itemstyle-width = "  xmlns: asp = "  > 
                 < itemtemplate>
                 < asp:linkbutton id = "  runat =  "  Causesvalidation = "  onclientclick = " 文本= "  commandargument = " <%#Eval("<​​/span> id  )%> " </ > 
                 </  itemtemplate  > 
                 
< itemstyle width = "  ">> span> </  itemstyle  > 
                 
                 </  asp:templatefield  >  



网格视图的行命令使用

 受保护的 无效 rowcmd(对象发​​件人,GridViewCommandEventArgs e)
    {
      // 创建并打开数据库连接
        尝试
        {
            //  int索引= Convert.ToInt32(e.CommandArgument); 
            //  GridViewRow row = GridView1.Rows [index]; 
             Int32  id = Convert.ToInt32(e.CommandArgument);
            字符串 comm = " ;
            SqlCommand cmd =  SqlCommand(comm,Db.GetConnection());
            cmd.Parameters.AddWithValue(" ,id);
            cmd.ExecuteNonQuery();
            //  ClientScript.RegisterStartupScript 
            // (GetType(),"Javascript","javascript:return alert('Deleted');;",true ); 
            GridView1.DataBind();
        }
        捕获(例外)
        {
        }

    } 


尝试这些链接,
如何向GridView添加删除按钮 [ ^ ]
要在删除时还添加确认按钮,
带有确认的GridView删除 [ http://aspknowledgebase.wordpress.com/2007 /06/22/add-confirmation-window-to-delete-button-in-gridview/ [



at next page to get table data i used :

protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {

            table_columns();

        }

    }
private void table_columns()
    {
        string x;
        x= Session["table_name"].ToString();
        string sql = "Select * from " + x+ "";
        SqlCommand cmd = new SqlCommand(sql, connstr);

        SqlDataAdapter dt = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        dt.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();


        connstr.Close();
    }

解决方案

You must set the DataKeyNames property in order for the automatic update and delete features of the GridView control to work.values of these key fields are passed to the data source control in order to specify the row to update or delete.



so set datakeyname first..


go thru this link
http://www.codedigest.com/Articles/ASPNET/212_How_to_edit_and_Delete_records_in_a_gridview_using_datakeys.aspx


add delete button as template field

 <asp:templatefield itemstyle-width="40px" xmlns:asp="#unknown">
                 <itemtemplate>
                 <asp:linkbutton id="lb1" runat="server" causesvalidation="false" onclientclick="return confirm('Are You sure to delete this entry')" text="Delete" commandargument="<%#Eval("id")%>"></asp:linkbutton>
                 </itemtemplate>
                 
<itemstyle width="40px"></itemstyle>
                 
                 </asp:templatefield>



on row command of grid view use

protected void rowcmd(object sender, GridViewCommandEventArgs e)
    {
      // create and open DB connection
        try
        {
            //int index = Convert.ToInt32(e.CommandArgument);
            //GridViewRow row = GridView1.Rows[index];
            Int32 id = Convert.ToInt32(e.CommandArgument);
            string comm = "Delete from tblComments where id=@id";
            SqlCommand cmd = new SqlCommand(comm, Db.GetConnection());
            cmd.Parameters.AddWithValue("id", id);
            cmd.ExecuteNonQuery();
            //ClientScript.RegisterStartupScript
            //(GetType(), "Javascript", "javascript: return alert('Deleted');; ", true);
            GridView1.DataBind();
        }
        catch (Exception ex)
        {
        }

    }


Try these links,
How to add delete button to GridView[^]
To also add a confirmation button on delete,
GridView Delete, with Confirmation[^]
http://aspknowledgebase.wordpress.com/2007/06/22/add-confirmation-window-to-delete-button-in-gridview/[^]


这篇关于帮助删除gridview?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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