home *** CD-ROM | disk | FTP | other *** search
- VERSION 5.00
- Object = "{00028C01-0000-0000-0000-000000000046}#1.0#0"; "DBGRID32.OCX"
- Begin VB.Form Form1
- Caption = "Unbound DBGrid Sample"
- ClientHeight = 3345
- ClientLeft = 2325
- ClientTop = 1815
- ClientWidth = 5910
- LinkTopic = "Form1"
- ScaleHeight = 3345
- ScaleWidth = 5910
- Begin MSDBGrid.DBGrid DBGrid1
- Height = 3240
- Left = 60
- OleObjectBlob = "UnBndGrd.frx":0000
- TabIndex = 0
- Top = 60
- Width = 5820
- End
- Attribute VB_Name = "Form1"
- Attribute VB_Base = "0{8BACA63C-DF27-11CF-B1A5-00AA003B0266}"
- Attribute VB_GlobalNameSpace = False
- Attribute VB_Creatable = False
- Attribute VB_TemplateDerived = False
- Attribute VB_PredeclaredId = True
- Attribute VB_Exposed = False
- Option Explicit
- ' Unbound DBGrid Sample
- ' The purpose of this sample is to give you an idea of what you have to do
- ' to get the DBGrid control working in unbound mode(DataMode = 1).
- ' For simplicity sake, this sample uses an array called UserData to store
- ' the data for the grid. In most cases, the DBGrid control can be bound
- ' to a Data control, however, some situations may require you to use it
- ' in unbound mode such as with a proprietary database format or for
- ' simple data sets for which there is no support by the data control.
- Private mTotalRows& ' Contains the total rows in the set of records
- Private UserData() As Variant ' 2-dimensional array containing records
- Private Const MAXCOLS = 3 ' Maximum number of fields in record set.
- ' When the user clicks on the Add icon, this subroutine adds a new
- ' row to the RowBuf variable and a bookmark to the NewRowBookmark
- ' variable
- Private Sub DBGrid1_UnboundAddData(ByVal RowBuf As RowBuffer, NewRowBookmark As Variant)
- Dim iCol As Integer
- mTotalRows = mTotalRows + 1
- ReDim Preserve UserData(MAXCOLS - 1, mTotalRows - 1)
- NewRowBookmark = mTotalRows - 1 'Sets the bookmark to the last row.
- ' The following loop adds a new record to the database.
- For iCol = 0 To UBound(UserData, 1)
- If Not IsNull(RowBuf.Value(0, iCol)) Then
- UserData(iCol, mTotalRows - 1) = RowBuf.Value(0, iCol)
- Else
- ' If no value set for column, then use the DefaultValue
- UserData(iCol, mTotalRows - 1) = DBGrid1.Columns(iCol).DefaultValue
- End If
- Next iCol
- End Sub
- ' This subroutine deletes a row based on it's bookmark.
- Private Sub DBGrid1_UnboundDeleteRow(Bookmark As Variant)
- Dim iCol As Integer, iRow As Integer
- ' Move all rows above the deleted row down in the
- ' array.
- For iRow = Bookmark + 1 To mTotalRows - 1
- For iCol = 0 To MAXCOLS - 1
- UserData(iCol, iRow - 1) = UserData(iCol, iRow)
- Next iCol
- Next iRow
- mTotalRows = mTotalRows - 1
- End Sub
- ' This subroutine is called, any time the DBGrid wants to display
- ' new data.
- Private Sub DBGrid1_UnboundReadData(ByVal RowBuf As RowBuffer, StartLocation As Variant, ByVal ReadPriorRows As Boolean)
- Dim CurRow&, iRow As Integer, iCol As Integer, iRowsFetched As Integer, iIncr As Integer
- ' DBGrid is requesting rows so give them to it
- If ReadPriorRows Then
- iIncr = -1
- iIncr = 1
- End If
- ' If StartLocation is Null then start reading at the end
- ' or beginning of the data set.
- If IsNull(StartLocation) Then
- If ReadPriorRows Then
- CurRow& = RowBuf.RowCount - 1
- Else
- CurRow& = 0
- End If
- ' Find the position to start reading based on the
- ' StartLocation bookmark and the iIncr variable
- CurRow& = CLng(StartLocation) + iIncr
- End If
- ' Transfer data from our data set array to the RowBuf object
- ' which DBGrid uses to display the data
- For iRow = 0 To RowBuf.RowCount - 1
- If CurRow& < 0 Or CurRow& >= mTotalRows& Then Exit For
- For iCol = 0 To UBound(UserData, 1)
- RowBuf.Value(iRow, iCol) = UserData(iCol, CurRow&)
- Next iCol
- ' Set bookmark using CurRow& which is also our
- ' array index
- RowBuf.Bookmark(iRow) = CStr(CurRow&)
- CurRow& = CurRow& + iIncr
- iRowsFetched = iRowsFetched + 1
- Next iRow
- RowBuf.RowCount = iRowsFetched
- End Sub
- ' This subroutine updates the data in the array after it has
- ' been edited.
- Private Sub DBGrid1_UnboundWriteData(ByVal RowBuf As RowBuffer, WriteLocation As Variant)
- Dim iCol As Integer
- ' Data is being updated
- ' Update each column in the data set array
- For iCol = 0 To MAXCOLS - 1
- If Not IsNull(RowBuf.Value(0, iCol)) Then
- UserData(iCol, WriteLocation) = RowBuf.Value(0, iCol)
- End If
- Next iCol
- End Sub
- Private Sub Form_Load()
- ' 3 Columns, 15 Rows of Data
- ReDim UserData(0 To 2, 0 To 14)
- ' Row 1
- UserData(0, 0) = "Jack"
- UserData(1, 0) = "Handey"
- UserData(2, 0) = "19"
- ' Row 2
- UserData(0, 1) = "Bill"
- UserData(1, 1) = "Haney"
- UserData(2, 1) = "12"
- ' Row 3
- UserData(0, 2) = "Brent"
- UserData(1, 2) = "Lainge"
- UserData(2, 2) = "33"
- ' Row 4
- UserData(0, 3) = "Tom"
- UserData(1, 3) = "Bodett"
- UserData(2, 3) = "40"
- ' Row 5
- UserData(0, 4) = "Laura"
- UserData(1, 4) = "Schlessinger"
- UserData(2, 4) = "42"
- ' Row 6
- UserData(0, 5) = "Mike"
- UserData(1, 5) = "Lanzorotta"
- UserData(2, 5) = "28"
- ' Row 7
- UserData(0, 6) = "Brad"
- UserData(1, 6) = "Doer"
- UserData(2, 6) = "29"
- ' Row 8
- UserData(0, 7) = "Kevin"
- UserData(1, 7) = "Graves"
- UserData(2, 7) = "42"
- ' Row 9
- UserData(0, 8) = "Julie"
- UserData(1, 8) = "Madson"
- UserData(2, 8) = "31"
- ' Row 10
- UserData(0, 9) = "John"
- UserData(1, 9) = "Clark"
- UserData(2, 9) = "30"
- ' Row 11
- UserData(0, 10) = "Sidney"
- UserData(1, 10) = "Houghton"
- UserData(2, 10) = "28"
- ' Row 12
- UserData(0, 11) = "Bart"
- UserData(1, 11) = "Sampson"
- UserData(2, 11) = "60"
- ' Row 13
- UserData(0, 12) = "Greg"
- UserData(1, 12) = "Price"
- UserData(2, 12) = "33"
- ' Row 14
- UserData(0, 13) = "Bobby"
- UserData(1, 13) = "Carnahan"
- UserData(2, 13) = "30"
- ' Row 15
- UserData(0, 14) = "Iris"
- UserData(1, 14) = "Nedlemeyer"
- UserData(2, 14) = "60"
- mTotalRows& = 15
- Dim oldcnt As Integer, newcnt As Integer
- Me.Show
- oldcnt = DBGrid1.Columns.Count
- newcnt = 0
- Dim i As Integer
- ' Remove old columns
- For i = DBGrid1.Columns.Count - 1 To 0 Step -1
- DBGrid1.Columns.Remove i
- Next i
- ' Add new columns
- For i = 0 To 2
- DBGrid1.Columns.Add newcnt
- DBGrid1.Columns(newcnt).Caption = "Col " & newcnt
- DBGrid1.Columns(newcnt).Visible = True
- newcnt = newcnt + 1
- Next i
- End Sub
-