Solved

GridView Paging Error using datasource in code

Posted on 2007-03-21
5
469 Views
Last Modified: 2013-11-07
Hi,

I'm using VS2005 ASP.NET, and I have a page with a GridView. In code behind, in the page load event, a method of one class is called and return a datatble, and this datatable is assigned to the datasource property of the gridview. Then databind is called.

The autogeneratecolumns is set to true. The returned datatable used as datasource have 13 columns, but only 6 shoud be showed in the gridview. So, I coded the "GridView1_RowDataBound" event and customized the cells of the grid and the headers text.

Everithing is working fine, until I set the paging property to true. When I try to use paging, a error occurs with the message :
"Argument  out of valid range. Parameter: index
in System.Web.UI.ControlCollection.get_Item(Int32 index) in  System.Web.UI.WebControls.TableCellCollection.get_Item(Int32 index)"

This error occurs inside the "GridView1_RowDataBound" event, but just occurs when I set pagging to true. If I set pagging to False everything works fine.

How can I fix this error ? I have to code the "GridView1_RowDataBound" event to customize my columns and headers because I set datasource by code ( a datatable returned by a class method ). But this error occurs inside the event when I try to use paging.

Can anyone help me please ?

Thanks in advance
0
Comment
Question by:Fiorilli
[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
5 Comments
 
LVL 28

Expert Comment

by:mmarinov
ID: 18769986
Can you please post your code from the RowDataBound?
It is obvious from your exception that you make some indexing. The problem can be because when you set Paginng to true then the datasource of the grid view does not contain all records and just the number that is set in the page size.

Martin
0
 

Author Comment

by:Fiorilli
ID: 18771120
Hi mmarinov,

here the RowDataBound code :

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

        If Session("dtbNotasMovimentos").Rows.Count = 0 Then Exit Sub

        Dim intColunas As Int16 = e.Row.Cells.Count
        Dim objRow As GridViewRow = e.Row
        Dim objCell As TableCell

        objCell = objRow.Cells(0)
        objCell.Height = New Unit(18)
        If objRow.RowType = DataControlRowType.Header Then objCell.Text = "Controle"
        objCell.Visible = False

        objCell = objRow.Cells(1)
        objCell.Height = New Unit(18)
        If objRow.RowType = DataControlRowType.Header Then objCell.Text = "Cod.Contrib."
        objCell.Visible = False

        objCell = objRow.Cells(2)
        objCell.Height = New Unit(18)
        If objRow.RowType = DataControlRowType.Header Then objCell.Text = "Empresa"
        objCell.HorizontalAlign = HorizontalAlign.Left
        objCell.Wrap = False

        objCell = objRow.Cells(3)
        objCell.Height = New Unit(18)
        If objRow.RowType = DataControlRowType.Header Then objCell.Text = "Cod.Declar."
        objCell.Visible = False

        objCell = objRow.Cells(4)
        objCell.Height = New Unit(18)
        If objRow.RowType = DataControlRowType.Header Then objCell.Text = "Declarante"
        objCell.Visible = False

        objCell = objRow.Cells(5)
        objCell.Height = New Unit(18)
        objCell.HorizontalAlign = HorizontalAlign.Center
        If objRow.RowType = DataControlRowType.DataRow Then
            objCell.Text = Format(Convert.ToDateTime(objCell.Text), "dd/MM/yyyy")
        ElseIf objRow.RowType = DataControlRowType.Header Then
            objCell.Text = "Lançado em"
        End If

        objCell = objRow.Cells(6)
        objCell.Height = New Unit(18)
        If objRow.RowType = DataControlRowType.Header Then objCell.Text = "Mês"
        objCell.HorizontalAlign = HorizontalAlign.Center

        objCell = objRow.Cells(7)
        objCell.Height = New Unit(18)
        If objRow.RowType = DataControlRowType.Header Then objCell.Text = "Ano"
        objCell.HorizontalAlign = HorizontalAlign.Center

        objCell = objRow.Cells(8)
        objCell.Height = New Unit(18)
        If objRow.RowType = DataControlRowType.Header Then objCell.Text = "Origem"
        objCell.Visible = False

        objCell = objRow.Cells(9)
        objCell.Height = New Unit(18)
        If objRow.RowType = DataControlRowType.Header Then objCell.Text = "Tipo Movimento"
        objCell.HorizontalAlign = HorizontalAlign.Left

        objCell = objRow.Cells(10)
        objCell.Height = New Unit(18)
        If objRow.RowType = DataControlRowType.Header Then objCell.Text = "Fechado"
        objCell.HorizontalAlign = HorizontalAlign.Center

        objCell = objRow.Cells(11)
        objCell.Height = New Unit(18)
        If objRow.RowType = DataControlRowType.Header Then objCell.Text = "Dívida"
        objCell.Visible = False

        objCell = objRow.Cells(12)
        objCell.Height = New Unit(18)
        If objRow.RowType = DataControlRowType.Header Then objCell.Text = "Parcela"
        objCell.Visible = False

    End Sub
 
Thanks in advance

0
 
LVL 28

Accepted Solution

by:
mmarinov earned 250 total points
ID: 18778093
I think you should surround your code in this if

If e.Row.RowType = DataControlRowType.DataRow Then
 'Put your code here
End If

Martin
0
 

Author Comment

by:Fiorilli
ID: 18779050
Hi mmarinov,

Looks like the index issue no longer occurs. But now a error 'PageIndexChanging' event was issued and a event handler not exist.
Another side effect is that the headers no more is showed correctly. I need to customize the headers texts from the cells, so to follow up your code I coded a elseif to tack the rowtype header and deal cell by cell basis like da datarow type, but is not working anymore.

What sould I code in the 'PageIndexChanging' event ?
How could I show th headers texts correctly ?

Thanks in advance
Best regards
0
 

Author Comment

by:Fiorilli
ID: 18779886
Hi mmarinov,

I correct the code sequence and solve the header problem. I coded the event seting the datasource and databind and the problem was corrected.

Everythinf is working fine now.. Thanks
0

Featured Post

Technology Partners: 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!

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…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

688 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