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

ASP.NET GridView RowCommand事件索引越界问题求助

解决ASP.NET GridView RowCommand事件中DataKeys索引越界问题

错误原因

核心问题是你的ButtonField控件未显式设置CommandArgument值,导致e.CommandArgument获取的不是有效的行索引,进而访问DataKeys[nInt]时超出集合范围。默认情况下,ButtonField不会自动将行索引作为CommandArgument传递。


解决方案

步骤1:修改GridView的ButtonField,添加CommandArgument

在每个asp:ButtonField标签中添加CommandArgument='<%# Container.DataItemIndex %>',让按钮点击时传递当前行的合法索引值。修改后的GridView代码如下:

<asp:GridView ID="gvUnitNomRoll" runat="server"  OnRowCommand ="gvUnitNomRoll_RowCommand1"  SkinID="gridviewSkin" Width="100%"
    DataKeyNames="id, name, desig, idr, service" EmptyDataText="" AutoGenerateColumns="false">
    <Columns>
        <asp:TemplateField HeaderText="Sl" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center">
            <ItemTemplate> <%#Ctype (Container, GridViewRow).RowIndex+1 %></ItemTemplate>
        </asp:TemplateField>
          <asp:BoundField HeaderText="id" DataField="name" Visible ="false"/>
        <asp:BoundField HeaderText="Name" DataField="name"/>
        <asp:BoundField HeaderText="desig" DataField="desig" />
        <asp:BoundField HeaderText="Idr" DataField="idr" />
        <asp:BoundField HeaderText="Service" DataField="service" />
       <asp:ButtonField ButtonType="Button" CommandName="ed" ControlStyle-CssClass ="allBtn blue"  HeaderText="Update Details" 
           ItemStyle-HorizontalAlign="Center" ItemStyle-Width="80px" Text="Update" CommandArgument='<%# Container.DataItemIndex %>' />                                        
        <asp:ButtonField ButtonType="Button" CommandName="tfrOut" ControlStyle-CssClass ="allBtn red"  
            HeaderText="Transfer" ItemStyle-HorizontalAlign="Center" ItemStyle-Width="80px" Text="Transfer Out" CommandArgument='<%# Container.DataItemIndex %>' />                                        
        <asp:ButtonField ButtonType="Button" CommandName="Verify" ControlStyle-CssClass ="allBtn green" 
            HeaderText="Verify" ItemStyle-HorizontalAlign="Center"  ItemStyle-Width="80px" Text="Verify" CommandArgument='<%# Container.DataItemIndex %>' />  
    </Columns>
</asp:GridView>

步骤2:优化VB.NET代码,增加索引有效性校验

为避免意外错误,在访问DataKeys前先校验索引的合法性:

Dim CmdName As String = e.CommandName
Dim nInt As Integer

' 先验证CommandArgument是否为有效整数
If Integer.TryParse(e.CommandArgument.ToString(), nInt) Then
    ' 校验索引是否在DataKeys集合的合法范围内
    If nInt >= 0 AndAlso nInt < gvUnitNomRoll.DataKeys.Count Then
        Dim currentPno As String = gvUnitNomRoll.DataKeys(nInt).Item("id").ToString()
        If CmdName = "ed" Then
            If currentPno <> "" Then
                ' 此处添加你的后续业务逻辑
            End If
        End If
    End If
End If

额外注意事项

  • 若GridView开启分页,Container.DataItemIndex会自动对应当前页的行索引,和DataKeys的索引保持一致,无需额外处理。
  • 确认DataKeyNames中指定的id字段确实存在于绑定的数据源中,避免后续获取字段值时出现新的错误。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.02 08:45:20