Sahil
Sun Dec 12 11:28:04 CST 2004
Good stuff man !! :)
- Sahil Malik
http://dotnetjunkies.com/weblog/sahilmalik
"n00b" <advinn@email.com> wrote in message
news:1102828643.774712.268790@c13g2000cwb.googlegroups.com...
>I have been thinking about the paging grid today when I saw the light.
> Consider Google, you type in some criteria and you get paged results,
> 10 at a time. The user has to click "next" to get the next page of 10.
> This is it! One has to replicate this behavior in a datagrid and this
> is what I came up with...
>
> 1. Create an inherited datagrid that removes the pesky little scroll
> bars as so:
>
> Public Class NSBDataGrid
> Inherits Windows.Forms.DataGrid
>
> Public Sub New()
>
> Me.HorizScrollBar.Width = 0
> Me.HorizScrollBar.Height = 0
> Me.VertScrollBar.Width = 0
> Me.VertScrollBar.Height = 0
>
> End Sub
>
> End Class
>
> 2. Use the data form wizard to generate a data form with a grid. Open
> the code window to the form and replace all instances of
> System.Windows.Forms.Datagrid with NSBDataGrid (NSB = No Scroll Bars.)
>
>
> 3. In the properties for the OleDbDataAdapter, change the SQL code in
> the select command text to start with SELECT TOP 25 instead of just
> SELECT.
>
> 4. Add two buttons to the form and name them btnUp and btnDown.
>
>
> 5. In my form, I was using a table called tblItemMaster with a Primary
> Key of ItemID (ItemID is an identity column.) My dataset was called
> objItemMaster. You'll have to replace the primary key in my btnDown
> event with your own and replace the objItemMaster with the dataset name
> on your form.
>
>
> This is my btnDown click event code:
>
> Private Sub btnDown_Click(ByVal sender As System.Object, ByVal e As
> System.EventArgs) Handles btnDown.Click
>
> Dim intMaxID As Integer
> Dim strSQL As String
> Dim intWherePos As Integer
>
> intMaxID =
> objItemMaster.Tables(0).Rows(objItemMaster.Tables(0).Rows.Count - 1)(0)
>
> strSQL = OleDbDataAdapter1.SelectCommand.CommandText
>
> intWherePos = InStr(strSQL, " WHERE")
>
> If intWherePos > 0 Then
>
> strSQL = Mid(strSQL, 1, intWherePos - 1)
>
> End If
>
> strSQL &= " WHERE ItemID > " & intMaxID
>
> OleDbDataAdapter1.SelectCommand.CommandText = strSQL
>
> LoadDataSet()
>
> End Sub
>
>
> This btnDown event gets the next 25 records and displays them in the
> datagrid.
>
>
> The btnUp is similar, but a bit more tricky.
>
> Private Sub btnUp_Click(ByVal sender As System.Object, ByVal e As
> System.EventArgs) Handles btnUp.Click
>
> Dim intMinID As Integer
> Dim strSQL As String
> Dim intWherePos As Integer
> Dim daMin As OleDb.OleDbDataAdapter
> Dim dsMin As New DataSet
> Dim intAnchorID As Integer
>
> intMinID = objItemMaster.Tables(0).Rows(0)(0)
>
> daMin = New OleDb.OleDbDataAdapter("SELECT MIN(T1.ItemID) FROM (SELECT
> TOP 25 ItemID FROM tblItemMaster2 WHERE ItemID < " & intMinID & " ORDER
> BY ItemID DESC) AS T1", OleDbConnection1)
>
> daMin.Fill(dsMin)
>
> If Not IsDBNull(dsMin.Tables(0).Rows(0)(0)) Then
>
> intAnchorID = dsMin.Tables(0).Rows(0)(0)
>
> strSQL = OleDbDataAdapter1.SelectCommand.CommandText
>
> intWherePos = InStr(strSQL, " WHERE")
>
> If intWherePos > 0 Then
>
> strSQL = Mid(strSQL, 1, intWherePos - 1)
>
> End If
>
> strSQL &= " WHERE ItemID >= " & intAnchorID
>
> OleDbDataAdapter1.SelectCommand.CommandText = strSQL
>
> LoadDataSet()
>
> End If
>
> End Sub
>
>
> Since you have to sort descending to get the previous 25 records, the
> datagrid would be sorted wrong, so I use a subquery to get the previous
> 25 primary keys, get the min ItemID from the subquery, then retrieve
> the 25 records in sorted order using the minimum as the anchor point.
>
> SELECT MIN(T1.ItemID) FROM (SELECT TOP 25 ItemID FROM tblItemMaster
> WHERE ItemID < 100 ORDER BY ItemID DESC) AS T1
>
> Tricky, but it works.
>
> Now the user can manually page the data and it won't download the whole
> table to the client machine all at one time.
>