>

여기에서 제공된 모든 솔루션을 살펴 봤지만 특정 문제를 해결하는 데 도움이되지 않는 것 같습니다.

격렬하게 작동하는 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

  • 답변 # 1

    아래 줄은 특정 행을 삭제하는 것 같습니다

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

    dt 카운트를 제거하십시오-1

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

  • 이전 파일에서 두 자리 숫자를 읽고 공백/문자열 피하기 (Java)
  • 다음 typescript - 마스터 임포트 파일이있는 Angular 2+