Solved

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

Posted on 2009-04-15
6
913 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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

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…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

760 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

22 Experts available now in Live!

Get 1:1 Help Now