删除Gridview中最后添加的行

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了删除Gridview中最后添加的行相关的知识,希望对你有一定的参考价值。

我在这里查看了所有提供的解决方案,但似乎没有一个对我的具体问题有帮助。

我在GridView中动态创建行,效果很好。

但是,我们想删除添加的最后一行。

我的代码是删除第一行的默认行。

我需要修改哪些想法以确保在用户单击“删除”按钮时仅删除最后添加的行?

//Markup

      <asp:gridview ID="Gridview1" RowStyle-Wrap="false" gridlines="None" CssClass="responsiveTable1" runat="server" ShowFooter="true" AutoGenerateColumns="false" OnRowDeleting="Gridview1_RowDeleting" OnRowCreated="Gridview1_RowCreated">
        <Columns>
        <asp:BoundField DataField="RowNumber" Visible="false" HeaderText="Row Number" />
        <asp:TemplateField HeaderText="Name">
         <headerstyle horizontalalign="Left" />
            <ItemTemplate>
                <asp:TextBox ID="txtsourcename" placeholder="Name...(e.g, Jane Doe)" runat="server" style="width:375px;" AutoPostBack="true" class="form-control textClass" OnTextChanged="txtsourcename_TextChanged"></asp:TextBox><br />
                <asp:CheckBox ID="grid1Details" ClientIDMode="Static" runat="server" Checked="false" AutoPostBack="true" OnCheckedChanged="Grid1CheckChanged" /><span style="color:#ff0000">*Check this box if N/A</span>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Address">
        <ItemStyle HorizontalAlign="Left"></ItemStyle>
            <ItemTemplate>
                <asp:TextBox ID="txtsourceaddress" placeholder="Address..." runat="server" style="width:375px;" class="form-control textClass"></asp:TextBox><br /><br />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="">
            <ItemTemplate>
             <asp:Button ID="ButtonAdd" runat="server" Text="Add" 
                    onclick="ButtonAdd_Click" CssClass="grvAddButton" /><br /><br /><br>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="">
            <ItemTemplate>
             <asp:Button ID="sourceDelete" runat="server" Text="Delete" CommandName="Delete"
                     CssClass="grvDelButton" OnClientClick="return confirm('Are you sure you want to remove this row?')"  /> <br /><br /><br />
            </ItemTemplate>
        </asp:TemplateField> 
        </Columns>
      </asp:gridview>

// VB

Protected Sub Gridview1_RowCreated(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
    If e.Row.RowType = DataControlRowType.DataRow Then
        Dim dt As DataTable = CType(ViewState("CurrentTable"), DataTable)
        Dim lb As Button = CType(e.Row.FindControl("sourceDelete"), Button)
        If lb IsNot Nothing Then
            If dt.Rows.Count > 1 Then
                If e.Row.RowIndex = dt.Rows.Count - 1 Then
                    lb.Visible = False
                End If
            Else
                lb.Visible = False
            End If
        End If
    End If
End Sub

    Protected Sub sourceDelete_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim lb As Button = CType(sender, Button)
        Dim gvRow As GridViewRow = CType(lb.NamingContainer, GridViewRow)
        Dim rowID As Integer = gvRow.RowIndex
        If ViewState("CurrentTable") IsNot Nothing Then
            Dim dt As DataTable = CType(ViewState("CurrentTable"), DataTable)
            If dt.Rows.Count > 1 Then
                If gvRow.RowIndex < dt.Rows.Count - 1 Then
                    dt.Rows.Remove(dt.Rows(rowID))
                    ResetRowID(dt)
                End If
            End If

            ViewState("CurrentTable") = dt
            Gridview1.DataSource = dt
            Gridview1.DataBind()
        End If

        SetPreviousData()
End Sub

    Private Sub ResetRowID(ByVal dt As DataTable)
        Dim rowNumber As Integer = 1
        If dt.Rows.Count > 0 Then
            For Each row As DataRow In dt.Rows
                row(0) = rowNumber
                rowNumber += 1
            Next
        End If
End Sub
答案

似乎下面的行删除了一个特定的行

dt.Rows.Remove(dt.Rows(rowID))

尝试删除dt - 1的计数

dt.Rows.Remove(dt.Rows(dt.Rows.count() -1))

以上是关于删除Gridview中最后添加的行的主要内容,如果未能解决你的问题,请参考以下文章

从 C# 添加新行时如何在 GridView 中添加删除链接?

c# asp.net GridView中如何删除一条记录后刷新页面

有啥方法可以在不同的行中显示 GridView 的列?

利用JavaScript选择GridView行

手动从gridview绑定中删除一行?

删除过滤器后,添加的行移动到最后一个位置