You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

VB中GridView动态添加行后删除最后一行的问题求助

Fix: Delete the Last Added Row in VB GridView

Hey there! It sounds like your dynamic row addition is working great, but the delete logic is targeting the first row instead of the most recently added one—let's fix that.

The core issue here is probably that your current delete code is hardcoding the first row index (like 0) instead of targeting the last row, or you're not updating the underlying data source that feeds your GridView (critical because GridView relies on its data source for state across postbacks). Here's how to adjust your code:

1. If you're directly modifying the GridView's Rows collection

If you're adding rows directly to the GridView without a separate data source (though using a data source like a DataTable is more reliable), update your delete button click handler to target the last row instead of the first:

Protected Sub btnDeleteLastRow_Click(sender As Object, e As EventArgs) Handles btnDeleteLastRow.Click
    ' Make sure there are rows to delete first to avoid errors
    If GridView1.Rows.Count > 0 Then
        ' Remove the last row (index is total rows minus 1, since indexes start at 0)
        GridView1.Rows.RemoveAt(GridView1.Rows.Count - 1)
    End If
End Sub

2. If you're using a data source (like DataTable stored in ViewState/Session)

This is the recommended approach because GridView loses dynamic row state on postback—so you're likely storing your rows in a DataTable saved to ViewState or Session. To delete the last row properly, you need to update this data source first, then rebind the GridView:

Protected Sub btnDeleteLastRow_Click(sender As Object, e As EventArgs) Handles btnDeleteLastRow.Click
    ' Retrieve your stored DataTable from ViewState
    Dim dynamicRowsTable As DataTable = CType(ViewState("DynamicGridViewRows"), DataTable)

    If dynamicRowsTable IsNot Nothing AndAlso dynamicRowsTable.Rows.Count > 0 Then
        ' Delete the last row from the DataTable
        dynamicRowsTable.Rows.RemoveAt(dynamicRowsTable.Rows.Count - 1)
        
        ' Update the stored DataTable and rebind the GridView
        ViewState("DynamicGridViewRows") = dynamicRowsTable
        GridView1.DataSource = dynamicRowsTable
        GridView1.DataBind()
    End If
End Sub

3. If you're using GridView's built-in RowDeleting event

If you're using a template column with a Delete button that triggers the RowDeleting event, override the default behavior to target the last row instead of the selected row:

Protected Sub GridView1_RowDeleting(sender As Object, e As GridViewDeleteEventArgs) Handles GridView1.RowDeleting
    ' Cancel the default delete action (which uses e.RowIndex)
    e.Cancel = True

    Dim dynamicRowsTable As DataTable = CType(ViewState("DynamicGridViewRows"), DataTable)
    
    If dynamicRowsTable IsNot Nothing AndAlso dynamicRowsTable.Rows.Count > 0 Then
        dynamicRowsTable.Rows.RemoveAt(dynamicRowsTable.Rows.Count - 1)
        ViewState("DynamicGridViewRows") = dynamicRowsTable
        GridView1.DataSource = dynamicRowsTable
        GridView1.DataBind()
    End If
End Sub

Key Notes:

  • Always add a check for Rows.Count > 0 to prevent IndexOutOfRangeException when there are no rows left to delete.
  • Using a data source (like DataTable) is far more reliable for dynamic GridView rows because it preserves state across postbacks—direct row manipulation might not persist after the page reloads.

内容的提问来源于stack exchange,提问作者Kenny

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 10:38:57