Solved

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

Posted on 2009-04-15
6
918 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
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.

 
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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

838 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