'Specified argument was out of the range of valid values. Parameter name: index
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
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
Solution 1:[1]
You need to add an extra check for the RowType, as shown below:
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |