Ciao amici,
ho un datagrid con paging e con pulsante delete.
Se mi trovo nella pagina x e cancello tutti gli elementi di questa pagina, dopo aver cancellato l'ultimo elemento mi compare
Valore di CurrentPageIndex non valido. Deve essere maggiore o uguale a zero e minore di PageCount.
Non dovrebbe cambiare pagina?
Vi posto un poò di codice
private sub fill_datagrid(strSQL as string)
'apro la connessione
dim objCmd as new OleDBDataAdapter(strSQL, objConn)
dim ds as new dataset
objCmd.Fill(ds, "Eventi")
dgLista.Datasource = ds.tables("Eventi").defaultview
dgLista.Databind()
end sub
sub cancella(obj as object, e as DataGridCommandEventargs)
strSQL = "DELETE FROM Eventi WHERE id = " & dgLista.DataKeys(CInt(e.Item.ItemIndex))
dim objCmd as new OleDBCommand(strSQL, objConn)
objCmd.Connection.Open()
objCmd.ExecuteNonQuery()
objCmd.Connection.Close()
fill_datagrid("SELECT * FROM Eventi ORDER BY ID DESC")
lblStatus.Text = "Evento cancellato"
end sub
sub cambia_pagina(obj as object, e as DataGridPageChangedEventArgs)
dgLista.CurrentPageIndex = e.NewPageIndex
fill_datagrid("SELECT * FROM Eventi ORDER BY ID DESC")
end sub
<asp:DataGrid id="dgLista" DataKeyField="id" runat="server" ForeColor="Black" CellPadding="4" BackColor="White" BorderColor="#CCCCCC" BorderWidth="1px" GridLines="Horizontal" BorderStyle="None" OnPageIndexChanged="cambia_pagina" AutoGenerateColumns="False" Width="680px" AllowPaging="True" CssClass="testo336699" OnDeleteCommand="cancella" PageSize="5">
<FooterStyle forecolor="Black" backcolor="#CCCC99"></FooterStyle>
<HeaderStyle font-size="7pt" font-names="verdana" font-bold="True" horizontalalign="Center" forecolor="White" backcolor="#336699"></HeaderStyle>
<PagerStyle font-size="7pt" font-names="verdana" font-bold="True" horizontalalign="Right" forecolor="#336699" position="TopAndBottom" backcolor="White" mode="NumericPages"></PagerStyle>
<SelectedItemStyle font-bold="True" forecolor="White" backcolor="#CC3333"></SelectedItemStyle>
<Columns>
<asp:BoundColumn DataField="id" HeaderText="Id Evento">
<HeaderStyle horizontalalign="Center" height="30px" width="60px"></HeaderStyle>
<ItemStyle horizontalalign="Center" cssclass="testo336699"></ItemStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="Titolo" HeaderText="Titolo">
<HeaderStyle horizontalalign="Center" height="30px"></HeaderStyle>
<ItemStyle horizontalalign="Center" cssclass="testo336699"></ItemStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="Data" HeaderText="Data">
<HeaderStyle horizontalalign="Center" height="30px" width="200px"></HeaderStyle>
<ItemStyle horizontalalign="Center" cssclass="testo336699"></ItemStyle>
</asp:BoundColumn>
<asp:TemplateColumn HeaderText="Modifica">
<ItemTemplate>
<table width="60" border="0" cellspacing="0" cellpadding="0" class="bleu336699g">
<tr>
<td width="60" height="30" align="center">
<asp:HyperLink ID="hlEdit" runat="server" Text="Modifica evento" NavigateUrl='<%#"edit.aspx?id="& container.dataitem("id")%>'></asp:HyperLink>
</td>
</tr>
</table>
</ItemTemplate>
</asp:TemplateColumn>
<asp:ButtonColumn Text="Elimina" CommandName="Delete"></asp:ButtonColumn>
</Columns>
</asp:DataGrid>
Grazie per l'aiuto!
Cià!