Click here to Skip to main content
15,892,697 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello friend i have a requirement in which i want to delete a row from my gridview on image button delete. i write the code like this
ASP.NET
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="false" Width="200" GridLines="None" OnRowCancelingEdit="GridView2_RowCancelingEdit" OnRowDeleting="GridView2_RowDeleting" >
                                                <Columns>
                                                    <asp:TemplateField HeaderText="Sl.No">
                                                        <ItemTemplate>
                                                            <%# Container.DataItemIndex + 1 %>
                                                        </ItemTemplate>
                                                    </asp:TemplateField>
                                                    <asp:TemplateField HeaderText="Supportiong Documents">
                                                        <ItemTemplate>
                                                            <asp:Label ID="lblSupportingDocument" runat="server" Text='<%#Eval("SupportingDocument") %>'></asp:Label>
                                                        </ItemTemplate>
                                                    </asp:TemplateField>
                                                    <asp:TemplateField HeaderText="Delete" ShowHeader="false">
                                                        <ItemTemplate>
                                                            <asp:ImageButton ID="imgbtnDelete" runat="server" ImageUrl="~/images/delete.png" CommandName="Cancel" />
                                                        </ItemTemplate>
                                                    </asp:TemplateField>

                                                </Columns>
                                            </asp:GridView>


and my code behind is
C#
protected void Page_Load(object sender, EventArgs e)
    {
      if (!IsPostBack)
        {
           if (string.IsNullOrEmpty(Request.QueryString["id"]) == false)
            {
                bind_SupportingDocumentGrid(id);

            }

        }
    }
void bind_SupportingDocumentGrid(int id)
    {
        List<TblFinancialTransactionSupportDocumentDetail> lstFTSD = ServiceAccess.GetProxy().GetAllFinancialTransactionSupportDocumentDetails();
        var x = (from y in lstFTSD
                 where y.FinancialTransactionId == id
                 select new
                 {
                     y.SupportingDocument
                 }).ToList();
        GridView2.DataSource = x;
        GridView2.DataBind();
    }
protected void GridView2_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        List<TblFinancialTransactionSupportDocumentDetail> lstFTSD = ServiceAccess.GetProxy().GetAllFinancialTransactionSupportDocumentDetails();
        Label lblSupportingDocument = (Label)GridView2.Rows[e.RowIndex].FindControl("lblSupportingDocument");
        var x = (from y in lstFTSD
                 where y.FinancialTransactionId == Convert.ToInt32(Request.QueryString["id"]) &&
                 y.SupportingDocument == (lblSupportingDocument).ToString()
                 select new
                 {
                     y.FinancialTransactionSupportDocumentDetailId
                 }).ToList();
        ServiceAccess.GetProxy().DeleteFinancialTransactionSupportDocumentDetail(Convert.ToInt32(x));
        bind_SupportingDocumentGrid(Convert.ToInt32(Request.QueryString["id"]));
    }


but somehow it is now working and i found using breakpoint is "GridView2_RowDeleting" event is not generating. kind help me to overcome from this problem.
thanks in advance.
:)
Posted

 
Share this answer
 
Its not going to execute coz u have not added any commandfields
C#
<asp:commandfield showdeletebutton="True" />


so u have to write that inside click event of image button

try it and let me know..
 
Share this answer
 
v2
XML
<ItemTemplate>
    <asp:ImageButton ID="imgbtnDelete" runat="server" ImageUrl="~/images/delete.png" CommandName="Cancel" />
</ItemTemplate>

As you are using ImageButton and you have a CommandName, so you should handle the GridView.RowCommand Event[^].

For that declare OnRowCommand in GridView Markup.
ASP.NET
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="false" Width="200" GridLines="None" OnRowCancelingEdit="GridView2_RowCancelingEdit" OnRowCommand="GridView2_RowCommand" >

Then define the Event on Code Behind like...
C#
protected void GridView2_RowCommand(Object sender, GridViewCommandEventArgs e)
{
    // If multiple buttons are used in a GridView control, use the
    // CommandName property to determine which button was clicked.
    if(e.CommandName == "Cancel")
    {
        // Write your Delete Logic.
    }
}
 
Share this answer
 
should have a button whose CommandName is "Delete":

C#
<asp:templatefield headertext="Delete" showheader="false" xmlns:asp="#unknown">
   <itemtemplate>
      <asp:imagebutton id="imgbtnDelete" runat="server" imageurl="~/images/delete.png" />
   </itemtemplate>
</asp:templatefield>
 
Share this answer
 
Refer :Asp.net gridview delete with imagebutton and javascript[^]

in the above link server side code is in vb.net.you can convert it into C# using Convert vb to c#/[^]
This may help.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900