Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 Development Tools
 ASP.NET
 Datagrid deletes via

Author  Topic 

bubberz
Constraint Violating Yak Guru

289 Posts

Posted - 2005-12-12 : 14:40:40
Here's my scenario:
1. Go to last page in datagrid
2. Click button to select the checkboxes next to all the rows in the data grid
3. Click button to delete all checked items
4. Error on page of:
Invalid CurrentPageIndex value. It must be >= 0 and < the PageCount
5. This doesn't happen if I click the "Delete" button for a row which would be the last record on the last page

Below is where the error happens, on DataGrid1.DataBind()


Sub BuildDataGrid()

Dim strSQLDG As String
strSQLDG = "SELECT ......"

'Create the command item
Dim sCon1 As New SqlConnection
sCon1.ConnectionString = Session("DBDDL")
sCon1.Open()
Dim objCmd As New SqlCommand(strSQLDG, sCon1)

'Create the DataAdapter
Dim DA As New SqlDataAdapter
DA.SelectCommand [/left]= objCmd

'Populate the DataSet and the connection
Dim DS As New DataSet
DA.Fill(DS)
sCon1.Close()

'Specifiy DataSource and call DataBind()
DataGrid1.DataSource = DS
DataGrid1.DataBind()

'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

End Sub

bubberz
Constraint Violating Yak Guru

289 Posts

Posted - 2005-12-12 : 16:58:26
Got it with.......

'Specifiy DataSource and call DataBind()
DataGrid1.DataSource = DS
Dim NumberOfItemsChecked As Integer
For Each dgI As DataGridItem In DataGrid1.Items
NumberOfItemsChecked = NumberOfItemsChecked + 1
Next

If DataGrid1.Items.Count Mod DataGrid1.PageSize = NumberOfItemsChecked And _
DataGrid1.CurrentPageIndex = DataGrid1.PageCount - 1 And _
DataGrid1.CurrentPageIndex <> 0 Then
DataGrid1.CurrentPageIndex = DataGrid1.CurrentPageIndex - 1
End If

DataGrid1.DataBind()
Go to Top of Page
   

- Advertisement -