如何在gridview中不使用OnRowDeleted / OnRowDeleting删除行

时间:2014-08-11 05:22:42

标签: c#

背后的代码

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 >

我的gridview中有2行需要使用上面的函数删除行。

抛出错误"gvsize" RowDeletingEvent was not handled properly.

是否有必要在gridview中使用OnRowDeleted/OnRowDeleting我觉得没必要?

2 个答案:

答案 0 :(得分:1)

How to delete row from gridview?

中所述
  

您正在从gridview中删除该行,但随后您将继续   再次调用数据绑定,这只是刷新gridview到   与原始数据源所在的状态相同。

     

从数据源中删除它,然后将数据绑定或数据绑定删除   并在不重新编辑的情况下将其从gridview中删除。

您可以使用行数据绑定事件来完成此任务。

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

并且在rowdatabound事件中你可以拥有

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

答案 1 :(得分:0)

尝试此操作删除行

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

您还可以使用Template

从其他方法中删除行

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#代码

 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();
                    }
             }

希望它可以帮到你