We have a "Check All" button on our datagrid, and if the user is on the last page of the datagrid, hits "Check All" and then hits the "Delete Checked Items", we get the following error (which doesn't happen if they delete all of the first or middle pages...only the last page):
Invalid CurrentPageIndex value. It must be >= 0 and < the PageCount.
Below is the code for the mass delete button click and the rebuilding of the datagrid:
'Loop through all the datagrid items
Dim Message As String = "You have selected: "
Try
sCon1.Open()
For Each dgI As DataGridItem In DataGrid1.Items
'get the checkbox
Dim chkSelected As CheckBox = DirectCast(dgI.FindControl("checkbox1"), CheckBox)
'If the checkbox is checked
If chkSelected.Checked Then
'add the product name to the message
Message &= dgI.Cells(16).Text & ", "
Dim intUId As Integer = dgI.Cells(16).Text
'*****
'If you want to delete items all you need to do is set the datagrid's datakey
'field to the tables primary key, then retreive the primary key here by
'Dim PrimaryKey As Integer = DataGrid1.DataKeys(dgI.ItemIndex)
'then run a DELETE FROM query deleting the item
Dim strSQLDelete As String
strSQLDelete = "Delete from ..."
Dim cmd As SqlCommand = New SqlCommand(strSQLDelete, sCon1)
myCommand.ExecuteNonQuery()
End If
Next
sCon1.Close()
Catch ex As Exception
lblStatus.Text = "Error Mass Delete: " & ex.Message
Finally
sCon1.Close()
'This is to control if the items deleted were on the last page
If DataGrid1.Items.Count Mod DataGrid1.PageSize = 1 And _
DataGrid1.CurrentPageIndex = DataGrid1.PageCount - 1 And _
DataGrid1.CurrentPageIndex <> 0 Then
DataGrid1.CurrentPageIndex = DataGrid1.CurrentPageIndex - 1
End If
Call BuildMainDG()
End Try
***********BuildMainDG
...do all SQL and connection stuff
Try
'Create the DataAdapter
Dim DA As New SqlDataAdapter
DA.SelectCommand = objCmd
'Populate the DataSet and the connection
Dim DS As New DataSet
DA.Fill(DS)
sCon1.Close()
DataGrid1.DataSource = DS
DataGrid1.DataBind()
Catch ex As Exception
lblStatus.Text = "Error: " & ex.Message
Finally
'This is to control if the items deleted were on the last page
If DataGrid1.Items.Count Mod DataGrid1.PageSize = 1 And _
DataGrid1.CurrentPageIndex = DataGrid1.PageCount - 1 And _
DataGrid1.CurrentPageIndex <> 0 Then
DataGrid1.CurrentPageIndex = DataGrid1.CurrentPageIndex - 1
End If
' end of controling deleted items on last datagrid page
sCon1.Close()
End Try
Invalid CurrentPageIndex value. It must be >= 0 and < the PageCount.
Below is the code for the mass delete button click and the rebuilding of the datagrid:
'Loop through all the datagrid items
Dim Message As String = "You have selected: "
Try
sCon1.Open()
For Each dgI As DataGridItem In DataGrid1.Items
'get the checkbox
Dim chkSelected As CheckBox = DirectCast(dgI.FindControl("checkbox1"), CheckBox)
'If the checkbox is checked
If chkSelected.Checked Then
'add the product name to the message
Message &= dgI.Cells(16).Text & ", "
Dim intUId As Integer = dgI.Cells(16).Text
'*****
'If you want to delete items all you need to do is set the datagrid's datakey
'field to the tables primary key, then retreive the primary key here by
'Dim PrimaryKey As Integer = DataGrid1.DataKeys(dgI.ItemIndex)
'then run a DELETE FROM query deleting the item
Dim strSQLDelete As String
strSQLDelete = "Delete from ..."
Dim cmd As SqlCommand = New SqlCommand(strSQLDelete, sCon1)
myCommand.ExecuteNonQuery()
End If
Next
sCon1.Close()
Catch ex As Exception
lblStatus.Text = "Error Mass Delete: " & ex.Message
Finally
sCon1.Close()
'This is to control if the items deleted were on the last page
If DataGrid1.Items.Count Mod DataGrid1.PageSize = 1 And _
DataGrid1.CurrentPageIndex = DataGrid1.PageCount - 1 And _
DataGrid1.CurrentPageIndex <> 0 Then
DataGrid1.CurrentPageIndex = DataGrid1.CurrentPageIndex - 1
End If
Call BuildMainDG()
End Try
***********BuildMainDG
...do all SQL and connection stuff
Try
'Create the DataAdapter
Dim DA As New SqlDataAdapter
DA.SelectCommand = objCmd
'Populate the DataSet and the connection
Dim DS As New DataSet
DA.Fill(DS)
sCon1.Close()
DataGrid1.DataSource = DS
DataGrid1.DataBind()
Catch ex As Exception
lblStatus.Text = "Error: " & ex.Message
Finally
'This is to control if the items deleted were on the last page
If DataGrid1.Items.Count Mod DataGrid1.PageSize = 1 And _
DataGrid1.CurrentPageIndex = DataGrid1.PageCount - 1 And _
DataGrid1.CurrentPageIndex <> 0 Then
DataGrid1.CurrentPageIndex = DataGrid1.CurrentPageIndex - 1
End If
' end of controling deleted items on last datagrid page
sCon1.Close()
End Try