Solved

GridView Paging Error using datasource in code

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

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…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

770 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