• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 475
  • Last Modified:

GridView Paging Error using datasource in code

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
Fiorilli
Asked:
Fiorilli
  • 3
  • 2
1 Solution
 
mmarinovCommented:
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
 
FiorilliAuthor Commented:
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
 
mmarinovCommented:
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
 
FiorilliAuthor Commented:
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
 
FiorilliAuthor Commented:
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!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now