Solved

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

Posted on 2009-04-15
6
916 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
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.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

832 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