【问题标题】:Specified argument was out of the range of valid values. Parameter name: index指定的参数超出了有效值的范围。参数名称:索引
【发布时间】:2014-08-13 03:29:39
【问题描述】:

我必须根据后面代码中条件的结果禁用网格视图中的超链接,但是我总是收到Specified argument was out of the range of valid values. Parameter name: index的错误

这是我的网格视图:

<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1" 
    AllowPaging="True" DataKeyNames="v_id" 
    AutoGenerateColumns="False" AllowSorting="True" CellPadding="4" 
    CssClass="gridview">
    <RowStyle BackColor="#EFF3FB"/>
        <Columns>
            <asp:BoundField DataField="ship_name" HeaderText="Vessel Name" SortExpression="ship_name">
            <HeaderStyle CssClass ="tblheader2" />
            </asp:BoundField>
            <asp:BoundField DataField="gross_tonnage" HeaderText="Gross Tonnage" SortExpression="gross_tonnage">
            <HeaderStyle CssClass ="tblheader2" />
            </asp:BoundField>
            <asp:BoundField DataField="regional_homeport" HeaderText="Region" SortExpression="region">
            <HeaderStyle CssClass ="tblheader2" />
            </asp:BoundField>
            <asp:BoundField DataField="owner_name" HeaderText="Owner" SortExpression="owner_name">
            <HeaderStyle CssClass ="tblheader2" />
            </asp:BoundField>
            <asp:BoundField DataField="cfvgl_validity_start" HeaderText="Date Issued" SortExpression="cfvgl_validity_start" DataFormatString="{0:d}">
            <HeaderStyle CssClass ="tblheader2" />
            </asp:BoundField>
            <asp:BoundField DataField="cfvgl_validity_end" HeaderText="Expiry Date" SortExpression="cfvgl_validity_end" DataFormatString="{0:d}">
            <HeaderStyle CssClass ="tblheader2" />
            </asp:BoundField>
             <asp:CommandField EditText="Select" ControlStyle-font-Underline="false" HeaderText ="View History" ControlStyle-forecolor="blue"  ShowSelectButton="True"  SelectText="History" ItemStyle-CssClass="links">
            <HeaderStyle  CssClass ="tblheader2"/>
            </asp:CommandField >

            <asp:TemplateField>
                <ItemTemplate >
                    <asp:HyperLink ID="cfvgl" runat ="server" Target ="_blank" NavigateUrl ='<%# Eval("v_id", "~/frqd/printBFARCFVGL.aspx?CFVGLVesselID={0}")%>' Font-Underline = "false" ForeColor ="blue" CssClass ="links" >CFVGL</asp:HyperLink>
                </ItemTemplate>
                <HeaderStyle CssClass ="tblheader2" />
            </asp:TemplateField>

            <asp:TemplateField>
                <ItemTemplate>
                     <%--FGRVesselID query  string of crystal report--%>
                    <asp:Hyperlink ID="fgr" runat="server" Target ="_blank" NavigateUrl='<%#Eval("v_id", "~/operator/printBFARFGR.aspx?FGRVesselID={0}") %>' Text ="FGR" font-underline="false" ForeColor="blue" CssClass="links"></asp:Hyperlink>
                </Itemtemplate>
                <HeaderStyle CssClass ="tblheader2" />
            </asp:TemplateField>

            <asp:TemplateField>
                    <ItemTemplate>
                        <asp:LinkButton ID="LinkButton2" runat="server" CssClass ="links" ForeColor ="blue" Font-Underline ="false" Width ="100px" PostBackUrl="#openModal">Add Violation</asp:LinkButton>
                    </ItemTemplate>
                    <HeaderStyle CssClass ="tblheader2" />
            </asp:TemplateField>

            <asp:BoundField DataField="vessel_type" HeaderText="Vessel Type" SortExpression="vessel_type">
            <HeaderStyle CssClass ="tblheader2" />
            </asp:BoundField>
        </Columns>
    <FooterStyle CssClass="gridviewfooter"/>
    <PagerStyle CssClass="gridviewfooter" ForeColor="White" HorizontalAlign="Center" />
    <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
    <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
    <EditRowStyle BackColor="#2461BF" />
    <AlternatingRowStyle BackColor="White" />
    <RowStyle CssClass="gridviewrow"/>
    <EmptyDataTemplate>There are no records to display.</EmptyDataTemplate>
</asp:GridView>

这是我的代码:

Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
    Dim vesseltype As String = e.Row.Cells(10).Text

    Select Case vesseltype
        Case 1
            e.Row.Cells(10).Text = "Catcher"
        Case 2
            e.Row.Cells(10).Text = "Carrier"
        Case 3
            e.Row.Cells(10).Text = "Escortboat"
        Case 4
            e.Row.Cells(10).Text = "Sonarboat"
        Case 5
            e.Row.Cells(10).Text = "Lightboat"
        Case 6
            e.Row.Cells(10).Text = "Ranger Boat"
        Case 7
            e.Row.Cells(10).Text = "Skiffboat"
        Case 8
            e.Row.Cells(10).Text = "Tanker"
    End Select

    If e.Row.Cells(10).Text = "Catcher" Then
        e.Row.Cells(9).Enabled = True
    Else
        e.Row.Cells(9).Enabled = False
        e.Row.Cells(9).Text = "N/A"
    End If
End Sub

【问题讨论】:

  • 哪一行报错?
  • 如果 e.Row.Cells(10).Text = "Catcher" 那么
  • 使用 e.Row.Cells.Count 检查您的细胞计数
  • 我已经检查了行数..行数匹配..但仍然有错误..
  • “匹配的行数”什么?

标签: asp.net vb.net


【解决方案1】:

需要为RowType添加额外的检查,如下图:

Protected Sub GridView1_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles GridView1.RowDataBound
    If e.Row.RowType = DataControlRowType.DataRow Then
        Dim vesseltype As String = e.Row.Cells(10).Text
        Select Case vesseltype
            Case 1
                e.Row.Cells(10).Text = "Catcher"
            Case 2
                e.Row.Cells(10).Text = "Carrier"
            Case 3
                e.Row.Cells(10).Text = "Escortboat"
            Case 4
                e.Row.Cells(10).Text = "Sonarboat"
            Case 5
                e.Row.Cells(10).Text = "Lightboat"
            Case 6
                e.Row.Cells(10).Text = "Ranger Boat"
            Case 7
                e.Row.Cells(10).Text = "Skiffboat"
            Case 8
                e.Row.Cells(10).Text = "Tanker"
        End Select

        If e.Row.Cells(10).Text = "Catcher" Then
            e.Row.Cells(9).Enabled = True

        Else
            e.Row.Cells(9).Enabled = False
            e.Row.Cells(9).Text = "N/A"
        End If
    End If
End Sub

否则,当它绑定 GridView.Footer 时,它会抛出“超出范围异常”,因为它没有与 DataRow 相同数量的单元格。

【讨论】:

  • 您能解释一下为什么需要检查RowType 吗?
  • 如果单元格 10 中的行类型文本不等于“catcher”,则 fgr 的超链接文本应更改为 N/A ..
  • @MikeS,需要 RowType 检查,以便将实现限制为仅 DataRow;否则,它将在 GridView.Footer 数据绑定事件期间引发异常。干杯!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多