SRIRAM RAMACHANDRAN
SRIRAM RAMACHANDRAN

Reputation: 337

How to delete a row without using OnRowDeleted/OnRowDeleting in gridview

Code Behind

public void lbDelete_Click(object sender, EventArgs e)
{
    LinkButton lb = sender as LinkButton;
    GridViewRow gvrow = lb.NamingContainer as GridViewRow;
    gvsize.DeleteRow(gvrow.RowIndex);
}

GridView:

<asp:GridView ID="gvsize" runat="server" ShowFooter="True" CellPadding="1" CellSpacing="2" AutoGenerateColumns="false" GridLines="Horizontal">
    <asp:TemplateField HeaderText="Action">
       <ItemTemplate>
          <asp:LinkButton ID="lnkdelete" runat="server" ForeColor="Blue" OnClick="lbDelete_Click">Delete</asp:LinkButton>
       </ItemTemplate>
    </asp:TemplateField>
</asp:GridView >

There are 2 rows in my gridview which I need to delete the row using the function above.

It throws an error "gvsize" RowDeletingEvent was not handled properly.

Is that necessary to use OnRowDeleted/OnRowDeleting in gridview which I feel not necessary??

Upvotes: 1

Views: 2698

Answers (2)

Vignesh Kumar A
Vignesh Kumar A

Reputation: 28413

As stated in How to delete row from gridview?

You are deleting the row from the gridview but you are then going and calling databind again which is just refreshing the gridview to the same state that the original datasource is in.

Either remove it from the datasource and then databind, or databind and remove it from the gridview without redatabinding.

You can use row databound event to accomplish this task.

  <asp:LinkButton ID="lnkBtnDel" runat="server" CommandName="DeleteRow" OnClientClick="return confirm('Are you sure you want to Delete this Record?');"">Delete</asp:LinkButton>

and in the rowdatabound event you can have

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "DeleteRow")
    {
        //incase you need the row index 
        int rowIndex = ((GridViewRow)((LinkButton)e.CommandSource).NamingContainer).RowIndex;
        //followed by your code 
    }
}

Upvotes: 1

Neeraj
Neeraj

Reputation: 4489

Try this to delete row

protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
dt.Rows.RemoveAt(e.RowIndex); 
GridView1.DataSource = dt;
GridView1.DataBind();
 }

You can also delete row from another method using Template Column

ASPX

<asp:TemplateField HeaderText="Delete">
                                <ItemTemplate>
                                <asp:ImageButton ID="imgDelete" runat="server" CommandName="deletenotice" ImageUrl="~/images/delete1.gif" alt="Delete"
                                    OnClientClick="return confirm('Are you sure want to delete the current record ?')">
                                    </asp:ImageButton>

                                </ItemTemplate>
                            </asp:TemplateField>

C# Code

 protected void gvNoticeBoardDetails_RowCommand(object sender, GridViewCommandEventArgs e)
    {
if (e.CommandName.ToLower().Equals("deletenotice"))
                {

                    GridViewRow row = (GridViewRow)(((ImageButton)e.CommandSource).NamingContainer);
                    NoticeBoard notice = new NoticeBoard();                       
                    HiddenField lblCust = (HiddenField)row.FindControl("hdngvMessageId");//Fetch the CourierId from the selected record

                    auditTrail.Action = DBAction.Delete;                       
                    Service simplyHRClient = new Service();
                    MessageClass messageClass = simplyHRClient.SaveNoticeBoard(notice, auditTrail);
                    if (messageClass.IsSuccess)
                    {

                        this.Page.AddValidationSummaryItem(messageClass.MessageText, "save");
                        showSummary.Style["display"] = string.Empty;
                        showSummary.Attributes["class"] = "success-message";


                        if (messageClass.RecordId != -1)
                            lblCust.Value = messageClass.RecordId.ToString();

                    }
                    else
                    {
                        this.Page.AddValidationSummaryItem(messageClass.MessageText, "save");
                        showSummary.Style["display"] = string.Empty;
                        showSummary.Attributes["class"] = "fail-message";
                    }
                    //Bind Again grid
                    GetAllNoticeBoard();
                    }
             }

Hope it helps you

Upvotes: 0

Related Questions