指定的参数超出了有效值的范围。参数名称:index

时间:2014-08-13 03:29:40

标签: asp.net vb.net

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

这是我的gridview:

<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

1 个答案:

答案 0 :(得分:0)

您需要为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时,它会抛出一个&#34;超出范围的异常&#34;因为它与DataRow没有相同数量的单元格。