删除Gridview中最后添加的行

问题描述 投票:0回答:1

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

我在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
asp.net vb.net gridview
1个回答
0
投票

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

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

尝试删除dt - 1的计数

dt.Rows.Remove(dt.Rows(dt.Rows.count() -1))
© www.soinside.com 2019 - 2024. All rights reserved.