【问题标题】:How to delete a row without using OnRowDeleted/OnRowDeleting in gridview如何在gridview中不使用OnRowDeleted/OnRowDeleting删除一行
【发布时间】:2014-08-11 05:22:42
【问题描述】:

后面的代码

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吗?我觉得没必要?

【问题讨论】:

标签: c#


【解决方案1】:

How to delete row from gridview?中所述

您正在从 gridview 中删除该行,但随后您将继续 再次调用 databind 只是将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 
    }
}

【讨论】:

  • 我先做了DataBind,那么如果需要删除该行呢?我将删除和DataBind..
  • @SRIRAMRAMACHANDRAN 如果是这样,您只需删除 DataSource 中的行并重新绑定它
  • 明白了.. 但是 gvsize.DeleteRow(gvrow.RowIndex) 这不可能吗?我发现了简单的任务
  • 好的,然后告诉我如何将 lblDelete_Click 映射到 OnRowDeleting 方法.. 我会这样做.. 我需要删除才能显示在所有行中。我将获得需要删除的 rowindex,并将该 rowindex 传递给删除行的 OnRowDeleting 事件。这可能吗??
【解决方案2】:

试试这个删除行

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

您还可以使用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#代码

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

希望对你有帮助

【讨论】:

  • 是否需要在Gridview中添加GridView1_RowDeleting方法?如 OnRowDeleting="GridView1_RowDeleting"
  • 是的,如果你使用OnRowDeleting
  • 不..我没有使用 OnRowDeleting... 我只是在所有行中都有删除链接按钮,当我单击删除时:它获取 rowIndex 并使用 gvsize[gvrow.RowIndex] 删除该行
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-04-23
  • 2010-10-10
  • 1970-01-01
  • 2010-09-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多