?
Solved

GridView Paging Error using datasource in code

Posted on 2007-03-21
5
Medium Priority
?
471 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 750 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

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
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…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

800 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