Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Index was out of range

Status
Not open for further replies.

taree

Technical User
May 31, 2008
316
US
I get the below error
I have a field that I bound with the gridview. when the value for the row is the same I just only display one row and ignore the other until the row value is different.

Code:
     <asp:BoundField  DataField="lettingdate" HeaderText="Letting Date"  >           
              <HeaderStyle Wrap="False" />
            <ItemStyle Wrap="False" />
            </asp:BoundField>

             <asp:TemplateField HeaderText="View Abstract (PDF)">              
             <ItemTemplate>             
                    <asp:HyperLink ID="abstractPDF" runat="server"   Target="_blank" Text='<%# Eval("contractid","Abstract (PDF)") %>' > </asp:HyperLink>
             </ItemTemplate>
                  <HeaderStyle Wrap="False" />
                  <ItemStyle Wrap="False" />
             </asp:TemplateField>


Partial Class abstractMenu
    Inherits System.Web.UI.Page
    Private previousCat As String = ""
    Private firstRow As Integer = -1
    Dim dsabstract As New DataSet



Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index

Source Error: 


Line 55: 
Line 56:             If previousCat = drv("lettingdate").ToString() Then
Line 57:                 If gvabstract.Rows(firstRow).Cells(0).RowSpan = 0 Then
Line 58:                     gvabstract.Rows(firstRow).Cells(0).RowSpan = 2
Line 59:                 Else
 

[code]
 
Code:
once I delete this line the code works fine. I wonder why though? can someone help to clarify this for me.
Handles gvabstract.RowDataBound





 Protected Sub gvabstract_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvabstract.RowDataBound



Code:
   <asp:GridView ID="gvabstract" runat="server"       
                    AutoGenerateColumns="False"  
                    GridLines="None" 
                    CssClass="mGrid"                             
                    HorizontalAlign="Left"    
                    OnRowDataBound = "gvabstract_RowDataBound"                      
                    AlternatingRowStyle-CssClass="alt" Width="100%" EnableModelValidation="True"
 
Mark,

I removed the line "OnRowDataBound = "gvabstract_RowDataBound" from here

Code:
 <asp:GridView ID="gvabstract" runat="server"                           AutoGenerateColumns="False"                      GridLines="None"                     CssClass="mGrid"                                                 HorizontalAlign="Left"                                                                  AlternatingRowStyle-CssClass="alt" Width="100%" EnableModelValidation="True"


and added back to the event and works without any problem.


Code:
Protected Sub gvabstract_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvabstract.RowDataBound
 
If you had the OnRowDataBound="gvabstract_RowDataBound and Handles gvabstract.RowDataBound action set then the event would fire twice maybe causing problems.

I suggest you set some breakpoints in the debugger on the event in question to test all of this out for yourself.

Mark,

Darlington Web Design[tab]|[tab]Experts, Information, Ideas & Knowledge[tab]|[tab]ASP.NET Tips & Tricks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top