Solved

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

Posted on 2009-04-15
6
933 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Command Line Tips and Tricks

The command line is a powerful tool at the disposal of every Linux user. Although Linux distros come with beautiful user interfaces, it's worthwhile to learn the command line because it allows you to do a number of things that you otherwise cannot do from the GUI.  

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…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…

617 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