从gridview中删除条目 [英] Delete entry from gridview

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

问题描述

我有gridview,其中我从数据库中获取记录,我在gridview列的右侧有两个链接按钮列。一个用于查看,第二个用于删除...我想当用户点击删除按钮然后相应的记录被删除。

I have gridview in which i am fetching records from database, i have two link buttons columns at the right side of my gridview columns. one is for view and second is for delete...i want when user click on the delete button then the corresponding record is delete.

<asp:GridView ID="gv_ads" runat="server" AutoGenerateColumns="False" 

                    onrowcommand="gv_ads_RowCommand" AllowPaging="True" 

                    OnPageIndexChanging="gv_ads_PageIndexChanging" CellPadding="4" 

                    ForeColor="#333333" GridLines="None" Width="998px" >
                    <AlternatingRowStyle BackColor="White" />
                   <Columns>
                           <asp:BoundField DataField="register_On" HeaderText="Register On" />
                           <asp:BoundField DataField="ad_id" HeaderText="Ad ID" />
                           <asp:BoundField DataField="category_name" HeaderText="Category" >
                           </asp:BoundField>
                           <asp:BoundField DataField="subcategory_name" HeaderText="Sub-Category" >
                           </asp:BoundField>
                           <asp:BoundField DataField="title" HeaderText="Ad Title" >
                           </asp:BoundField>
                           <asp:BoundField DataField="s_name" HeaderText="State" >
                           </asp:BoundField>
                           <asp:BoundField DataField="pincode" HeaderText="Pincode" />
                           
                           <asp:TemplateField HeaderText="View Ad">
                            <ItemTemplate>
                            <asp:LinkButton runat="server" ID="lnkView" CommandArgument='<%#Eval("ad_id") %>' CommandName="VIEW">View/Edit</asp:LinkButton>
                           </ItemTemplate>
                           </asp:TemplateField>

                           <asp:TemplateField HeaderText="Delete Ad">
                           <ItemTemplate>
                             <asp:LinkButton runat="server" ID="linkDelete" OnClientClick="return confirm('Are you sure you want to delete this entry?');" CommandArgument='<%#Eval("ad_id") %>' CommandName="DELETE">Delete</asp:LinkButton>
                           </ItemTemplate>
                           </asp:TemplateField>
                          
                   </Columns>
                    <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
                    <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
                    <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
                    <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
                    <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
                    <SortedAscendingCellStyle BackColor="#FDF5AC" />
                    <SortedAscendingHeaderStyle BackColor="#4D0000" />
                    <SortedDescendingCellStyle BackColor="#FCF6C0" />
                    <SortedDescendingHeaderStyle BackColor="#820000" />
                </asp:GridView>




protected void gv_ads_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "VIEW")
        {
            int index = Convert.ToInt32(e.CommandArgument);
            Response.Redirect("Ads_Details.aspx?ad_id=" + index);

        }
        else if (e.CommandName == "DELETE")
        {
            int index = Convert.ToInt32(e.CommandArgument);
            SqlCommand cmd = new SqlCommand("sps_deletead", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@useremail", ses);
            cmd.Parameters.AddWithValue("@ad_id", index);
            try
            {
                con.Open();
                cmd.ExecuteNonQuery();
                message = "Ad Deleted Successfully.";
                ClientScript.RegisterStartupScript(GetType(), "alert", "alert('" + message + "');", true);
                con.Close();
                BindGridview();
            }
            catch (Exception)
            {
                throw;
            }
        }
    }



当我点击删除按钮时,记录从数据库中删除但出现错误GridView'gv_ads 已解决的事件RowDeleting未处理。

我的代码工作正常,但为什么会出现此错误。


when i click on delete button the record is deleted from database but error comes "The GridView 'gv_ads' fired event RowDeleting which wasn't handled."
My code is working fine but Why this error is coming.

推荐答案

更改命令删除到其他名称,如AdsDelete,并更改您的条件

change command "DELETE" to some other name like "AdsDelete" and change your condition as well
 else if (e.CommandName == "AdsDelete")
{



或者您可以将事件 OnRowDeleting =gv_ads_RowDeleting添加到您的网格中,如下所示




Or you can add the event OnRowDeleting="gv_ads_RowDeleting" to your grid like below

<asp:gridview id="gv_ads" runat="server" onrowdeleting="gv_ads_RowDeleting" xmlns:asp="#unknown"></asp:gridview>



生成事件代码或编写如下事件


generate the event code or write the event as below

public void gv_ads_RowDeleting(Object sender, GridViewDeleteEventArgs e)
{

} 


在Gridview中点击此事件。





Just Fire this Event in your Gridview.


<asp:gridview id="GridView1" runat="server" xmlns:asp="#unknown">OnRowDeleting="GridView1_RowDeleting">
     </asp:gridview>







protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
       {
//leave it empty

       }


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

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