vb.net 指定的参数超出了有效值的范围。参数名称:索引
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25277565/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Specified argument was out of the range of valid values. Parameter name: index
提问by Danbo
I have to disable the hyperlink in the gridview depending on the result of the condition in my code behind, however i always get an error of Specified argument was out of the range of valid values. Parameter name: index
我必须根据后面代码中的条件结果禁用 gridview 中的超链接,但是我总是收到错误 Specified argument was out of the range of valid values. Parameter name: index
here is my 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>
Here is my code behind:
这是我的代码:
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
回答by Christian
You need to add an extra check for the RowType, as shown below:
您需要为 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
Otherwise, when it binds the GridView.Footer, it'll throw an "out of range exception" as it doesn't have the same number of cells as the DataRow.
否则,当它绑定 GridView.Footer 时,它会抛出“超出范围异常”,因为它与 DataRow 的单元格数量不同。

