Solved

error:( while paging in gridview..)::Specified argument was out of the range of valid values..

Posted on 2009-04-15
6
914 Views
Last Modified: 2013-11-07
After , making alllow paging = true to the gridview, i get this error...
Specified argument was out of the range of valid values.
Parameter name: index
on this line...
e.Row.Cells(1).Visible = False in the rowdatabound event...
I had set , paging size to 5 . how can i solve this issue..?

this is the code block , which is creatin the error..
Protected Sub GridView2_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView2.RowDataBound

        e.Row.Cells(1).Visible = False
        Dim delbutton As LinkButton = TryCast(e.Row.FindControl("Button1"), LinkButton)
        Response.Write(e.Row.Cells(1).Text)
        If e.Row.Cells(1).Text = "1" Then
            delbutton.Enabled = False
        ElseIf e.Row.Cells(1).Text = "0" Then
            delbutton.Enabled = True
        End If
    End Sub
---------------

<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" AllowPaging="True" PageSize="5" DataKeyNames="trid">

            <Columns>

                  <asp:BoundField DataField="name" HeaderText="name" />

                <asp:BoundField DataField="chk" HeaderText="Chk"  />

                <asp:TemplateField ShowHeader="False">

                    <ItemTemplate>

                        <asp:LinkButton ID="Button1" runat="server" CausesValidation="false" CommandName="Delete"

                            Text="Delete" />

                    </ItemTemplate>

                     </asp:TemplateField>

            </Columns>

        </asp:GridView>
 

 Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Dim constr As String = ConfigurationManager.AppSettings("con")

        con = New SqlConnection(constr)

        Cmd = New SqlCommand()

        If Not IsPostBack Then

            BindToGrid()

        End If
 

    End Sub

    Private Sub BindToGrid()

        Cmd = New SqlCommand("Select * from tbl1", con)

        Dim ds As New DataSet()

        Dim da As New SqlDataAdapter(Cmd)

        da.Fill(ds)

        GridView2.DataSource = ds

        GridView2.DataBind()

    End Sub
 

   

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

        e.Row.Cells(1).Visible = False

        Dim delbutton As LinkButton = TryCast(e.Row.FindControl("Button1"), LinkButton)

        Response.Write(e.Row.Cells(1).Text)

        If e.Row.Cells(1).Text = "1" Then

            delbutton.Enabled = False

        ElseIf e.Row.Cells(1).Text = "0" Then

            delbutton.Enabled = True

        End If

    End Sub

 
 

    Protected Sub GridView2_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles GridView2.PageIndexChanging

        GridView2.PageIndex = e.NewPageIndex()

        BindToGrid()

    End Sub

Open in new window

0
Comment
Question by:Rajar Ahmed
  • 3
  • 2
6 Comments
 
LVL 18

Assisted Solution

by:carlnorrbom
carlnorrbom earned 150 total points
ID: 24146760
Hi,

I suspect it is related to the fact that You're getting "non-data" rows into the row collection of the GridView, try to change Your code to:

        If e.Row.RowType = DataControlRowType.DataRow Then
            e.Row.Cells(1).Visible = False
        End If

Because when You hit the row containing the pager controls, the number of cells will not be the same as for the datarows, hence the "Argument out of range" error.

/Carl.
0
 
LVL 14

Expert Comment

by:GiftsonDJohn
ID: 24146948
You will face problem over here

Protected Sub GridView2_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView2.RowDataBound
 
        e.Row.Cells(1).Visible = False
        Dim delbutton As LinkButton = TryCast(e.Row.FindControl("Button1"), LinkButton)
        Response.Write(e.Row.Cells(1).Text)
        If e.Row.Cells(1).Text = "1" Then
            delbutton.Enabled = False
        ElseIf e.Row.Cells(1).Text = "0" Then
            delbutton.Enabled = True
        End If
    End Sub

The RowDataBound Event will fire for Header, Footer and DataRow. you have to make sure that you are validating only for DataRow.

Protected Sub GridView2_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView2.RowDataBound
   If e.Row.RowType= DataControlRowType.DataRow Then
        e.Row.Cells(1).Visible = False
        Dim delbutton As LinkButton = TryCast(e.Row.FindControl("Button1"), LinkButton)
        Response.Write(e.Row.Cells(1).Text)
        If e.Row.Cells(1).Text = "1" Then
            delbutton.Enabled = False
        ElseIf e.Row.Cells(1).Text = "0" Then
            delbutton.Enabled = True
        End If
  End If
    End Sub
0
 
LVL 18

Author Comment

by:Rajar Ahmed
ID: 24147890
worked..but it created  column of chk in the header ... which is unnecc..

i removed headertext of chk...but that space remained in the header....my header of gridview lost it design.
.luks untidy........

0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 14

Expert Comment

by:GiftsonDJohn
ID: 24150289
okay put the line

e.Row.Cells(1).Visible = False

outside the if condition


Protected Sub GridView2_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView2.RowDataBound
        e.Row.Cells(1).Visible = False
   If e.Row.RowType= DataControlRowType.DataRow Then
        Dim delbutton As LinkButton = TryCast(e.Row.FindControl("Button1"), LinkButton)
        Response.Write(e.Row.Cells(1).Text)
        If e.Row.Cells(1).Text = "1" Then
            delbutton.Enabled = False
        ElseIf e.Row.Cells(1).Text = "0" Then
            delbutton.Enabled = True
        End If
  End If
    End Sub

0
 
LVL 18

Author Comment

by:Rajar Ahmed
ID: 24150480
on tryin the above , i get this error  again.....

Specified argument was out of the range of valid values.
Parameter name: index
in this line
 e.Row.Cells(1).Visible = False

0
 
LVL 14

Accepted Solution

by:
GiftsonDJohn earned 350 total points
ID: 24151095
It seems like pager is also active. So I'm adding another condition

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

If e.Row.RowType=DataControlRowType.DataRow Or e.Row.RowType=DataControlRowType.Header Then
        e.Row.Cells(1).Visible = False
   If e.Row.RowType= DataControlRowType.DataRow Then
        Dim delbutton As LinkButton = TryCast(e.Row.FindControl("Button1"), LinkButton)
        Response.Write(e.Row.Cells(1).Text)
        If e.Row.Cells(1).Text = "1" Then
            delbutton.Enabled = False
        ElseIf e.Row.Cells(1).Text = "0" Then
            delbutton.Enabled = True
        End If
  End If
End If
    End Sub
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Export import database 4 44
Help with Syntax 9 29
Show/Hide ASP.NET controls with JQuery best practices 3 21
Gridview alignment 1 12
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

27 Experts available now in Live!

Get 1:1 Help Now