Porblema con un datagrid

29/07/2004 - 01:04 por Roberto Londono | Informe spam
Hola amigos, tengo un problema con el datagrid.
Yo cree un datagrid y le adicion un boton Select y Paging

Mi problema es que cuando yo tengo mas de 10 elementos en
el datagrid el me genera una nueva pagina. Yo intengo
selecionarla y me ejecuta le boton de select.
Pero si yo ejecuto el boton de select en los primeros 10
elementos (primera pagina) no me da ningun error.

El error que sale cuando trato de cambiar la pagina es el
siguiente.

Specified argument was out of the range of valid values.
Parameter name: index
Description: An unhandled exception occurred during the
execution of the current web request. Please review the
stack trace for more information about the error and
where it originated in the code.

Exception Details: System.ArgumentOutOfRangeException:
Specified argument was out of the range of valid values.
Parameter name: index

Source Error:


Line 312:
Line 313:
Line 314: CodCard = e.Item.Cells(1).Text
Line 315: TBCardNo.Text = CodCard
Line 316:

Y El codigo que esta utilizando es:
Paging :


Private Sub MasterGrid_PageIndexChanged1(ByVal source As
Object, ByVal e As
System.Web.UI.WebControls.DataGridPageChangedEventArgs)
Handles MasterGrid.PageIndexChanged


Me.SqlDataAdapter1.SelectCommand.Parameters.Add
("@Date_Need1", System.Data.SqlDbType.DateTime, 8).Value
= TBDate1.Text

Me.SqlDataAdapter1.SelectCommand.Parameters.Add
("@Date_Need2", System.Data.SqlDbType.DateTime, 8).Value
= TBDate2.Text

'MasterGrid.DataSource = GetLogVisitor
(TBDate1.Text, TBDate2.Text)

MasterGrid.CurrentPageIndex = e.NewPageIndex
MasterGrid.DataSource =
MeS_HR_DS_View_IdenticationVisitorlog1
SqlDataAdapter1.Fill
(MeS_HR_DS_View_IdenticationVisitorlog1)
MasterGrid.DataBind()
MasterGrid.Enabled = True
End Sub

*******************
Boton Select
*********************************************************


Private Sub MasterGrid_ItemCommand1(ByVal source As
Object, ByVal e As
System.Web.UI.WebControls.DataGridCommandEventArgs)
Handles MasterGrid.ItemCommand
'Dim Itm As Integer

' Itm = e.Item.ItemIndex()
'Dim keys As DataKeyCollection
'keys = MasterGrid.DataKeys

Dim CodCard As String
'CodCard = Me.MasterGrid.SelectedItem.Cells
(0).Text
CodCard = e.Item.Cells(1).Text
TBCardNo.Text = CodCard
MasterGrid.Enabled = True
DGDetail.DataSource = GetVisitorDetail
(TBDate1.Text, TBDate2.Text, TBCardNo.Text)
' SqlDataAdapter1.Fill
(MeS_HR_DS_ViewIdenticationVisitorlog1)
DGDetail.DataBind()
DGDetail.Enabled = True
End Sub
 

Leer las respuestas

#1 GastonQ
29/07/2004 - 04:43 | Informe spam
Hola, creo saber que es lo que te está pasando.

Tienes que hacer una validación al cambiar la propiedad CurrentPageIndex.

Esto se debe a que si vuelves a cargar el DataGrid, con el resultado de una
consulta que tiene menos registros que la consulta inicial, se produce este
error que mencionas. Básicamente la validación es que si la página actual es
mayor que el total de registros del DataGrid dividido por el valor de la
propiedad PageSize, entonces debes volver a la página 1.

Te paso un metodito que hice para un proyecto:

public void CheckPaging( DataGrid dgSource, DataSet dtsSource, string
tableName )
{
if( dgSource.AllowPaging )
{
// Si la página no existe retorna la primera.
if( dgSource.CurrentPageIndex > ( dtsSource.Tables[
tableName ].Rows.Count / dgSource.PageSize ) )
{
dgSource.CurrentPageIndex = 0;
}
}
}

Saludos
Gaston Quirque
Microsoft MVP


"Roberto Londono" escribió en el
mensaje news:62ef01c474f7$3177b790$
Hola amigos, tengo un problema con el datagrid.
Yo cree un datagrid y le adicion un boton Select y Paging

Mi problema es que cuando yo tengo mas de 10 elementos en
el datagrid el me genera una nueva pagina. Yo intengo
selecionarla y me ejecuta le boton de select.
Pero si yo ejecuto el boton de select en los primeros 10
elementos (primera pagina) no me da ningun error.

El error que sale cuando trato de cambiar la pagina es el
siguiente.

Specified argument was out of the range of valid values.
Parameter name: index
Description: An unhandled exception occurred during the
execution of the current web request. Please review the
stack trace for more information about the error and
where it originated in the code.

Exception Details: System.ArgumentOutOfRangeException:
Specified argument was out of the range of valid values.
Parameter name: index

Source Error:


Line 312:
Line 313:
Line 314: CodCard = e.Item.Cells(1).Text
Line 315: TBCardNo.Text = CodCard
Line 316:

Y El codigo que esta utilizando es:
Paging :


Private Sub MasterGrid_PageIndexChanged1(ByVal source As
Object, ByVal e As
System.Web.UI.WebControls.DataGridPageChangedEventArgs)
Handles MasterGrid.PageIndexChanged


Me.SqlDataAdapter1.SelectCommand.Parameters.Add
("@Date_Need1", System.Data.SqlDbType.DateTime, 8).Value
= TBDate1.Text

Me.SqlDataAdapter1.SelectCommand.Parameters.Add
("@Date_Need2", System.Data.SqlDbType.DateTime, 8).Value
= TBDate2.Text

'MasterGrid.DataSource = GetLogVisitor
(TBDate1.Text, TBDate2.Text)

MasterGrid.CurrentPageIndex = e.NewPageIndex
MasterGrid.DataSource > MeS_HR_DS_View_IdenticationVisitorlog1
SqlDataAdapter1.Fill
(MeS_HR_DS_View_IdenticationVisitorlog1)
MasterGrid.DataBind()
MasterGrid.Enabled = True
End Sub

*******************
Boton Select
*********************************************************


Private Sub MasterGrid_ItemCommand1(ByVal source As
Object, ByVal e As
System.Web.UI.WebControls.DataGridCommandEventArgs)
Handles MasterGrid.ItemCommand
'Dim Itm As Integer

' Itm = e.Item.ItemIndex()
'Dim keys As DataKeyCollection
'keys = MasterGrid.DataKeys

Dim CodCard As String
'CodCard = Me.MasterGrid.SelectedItem.Cells
(0).Text
CodCard = e.Item.Cells(1).Text
TBCardNo.Text = CodCard
MasterGrid.Enabled = True
DGDetail.DataSource = GetVisitorDetail
(TBDate1.Text, TBDate2.Text, TBCardNo.Text)
' SqlDataAdapter1.Fill
(MeS_HR_DS_ViewIdenticationVisitorlog1)
DGDetail.DataBind()
DGDetail.Enabled = True
End Sub

Preguntas similares