如何在GridView中删除记录 [英] How to delete record in gridview

查看:85
本文介绍了如何在GridView中删除记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何示例代码如何通过单击删除"按钮删除gridview中的记录?

Any sample code how to delete record in gridview by click on the "delete" button?

推荐答案

尝试
带有确认的GridView删除 [ http://msdn.microsoft.com/en-us/library/ms972940.aspx [ ^ ]
http://csharpdotnetfreak.blogspot.com/2009/04/delete-multiple- rows-gridview-checkbox.html [ ^ ]
http://csharpdotnetfreak.blogspot.com/2009/05/gridview-sqldatasource-insert- edit.html [ ^ ]

第三个链接可能对您最有用.
Try
GridView Delete, with Confirmation[^]
http://msdn.microsoft.com/en-us/library/ms972940.aspx[^]
http://csharpdotnetfreak.blogspot.com/2009/04/delete-multiple-rows-gridview-checkbox.html[^]
http://csharpdotnetfreak.blogspot.com/2009/05/gridview-sqldatasource-insert-edit.html[^]

The third link maybe most useful to you.


使用模板字段并将ID作为CommandArgument传递

use template field and pass id as CommandArgument

<asp:templatefield 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>
                 
                 </asp:templatefield>



和gridview的行命令



and on row command of gridview

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)
       {
       }

   }


您好,亲爱的,

在gridview中,当您单击删除按钮(即自动生成的列)时,获得该行的ID表示该行的唯一ID.
如果没有唯一的ID,则将其绑定到隐藏字段中.并在删除按钮的点击事件上获取该ID.并执行删除查询.像这样:
hello dear,

in your gridview , when you click on the delete button (if i.e. auto generated column) get the id of that row means unique id of that row.
If there is no unique id then bind it in hidden field. and get that id on the click event of delete button. and execute delete query. like this :
string str = GridView1.DataKeys[e.NewSelectedIndex].Value.ToString();
SqlCommand cmd = new SqlCommand("delete from tablename where id=@id", con);
cmd.Parameters.AddWithValue("@id", str);
con.Open();
cmd.ExecuteNonQuery();
con.Close();



并再次绑定网格.

希望对您有帮助.
如果有帮助,别忘了将其标记为答案. :)



and bind grid again.

Hope this will help you.
And Don''t forget to mark as answer if it helps. :)


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

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