Solved

GridView Paging Error using datasource in code

Posted on 2007-03-21
5
463 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
  • 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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

705 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now