Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Mass deleted on DG last page gives invalid currentpageindex value 2

Status
Not open for further replies.

codecomm

Programmer
Feb 14, 2007
121
US
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
 
You'll have to trace through your code. Your logic for accounting if it is on the last page is not working. I am not sure why you have this line of code:
Code:
 If DataGrid1.Items.Count Mod DataGrid1.PageSize = 1 And
 
remove this part of the if/then statment [tt]DataGrid1.Items.Count Mod DataGrid1.PageSize = 1[/tt] all you need to know if the currentpageindex == totalpages - 1. if so, decrement by 1.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
jbenson001 / jmeckley,

Thanks..that did the trick.

I wonder why having that same code works when you click the "Delete" link for each line in the datagrid?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top