home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Visual Database / Visual BASIC 5.0 (Ent. Edition) / Vb5ent Extractor.EXE / VB / SAMPLES / MISC / UNBNDGRD / UNBNDGRD.FRM (.txt) next >
Encoding:
Visual Basic Form  |  1996-09-16  |  6.4 KB  |  198 lines

  1. VERSION 5.00
  2. Object = "{00028C01-0000-0000-0000-000000000046}#1.0#0"; "DBGRID32.OCX"
  3. Begin VB.Form Form1 
  4.    Caption         =   "Unbound DBGrid Sample"
  5.    ClientHeight    =   3345
  6.    ClientLeft      =   2325
  7.    ClientTop       =   1815
  8.    ClientWidth     =   5910
  9.    LinkTopic       =   "Form1"
  10.    ScaleHeight     =   3345
  11.    ScaleWidth      =   5910
  12.    Begin MSDBGrid.DBGrid DBGrid1 
  13.       Height          =   3240
  14.       Left            =   60
  15.       OleObjectBlob   =   "UnBndGrd.frx":0000
  16.       TabIndex        =   0
  17.       Top             =   60
  18.       Width           =   5820
  19.    End
  20. Attribute VB_Name = "Form1"
  21. Attribute VB_Base = "0{8BACA63C-DF27-11CF-B1A5-00AA003B0266}"
  22. Attribute VB_GlobalNameSpace = False
  23. Attribute VB_Creatable = False
  24. Attribute VB_TemplateDerived = False
  25. Attribute VB_PredeclaredId = True
  26. Attribute VB_Exposed = False
  27. Option Explicit
  28. ' Unbound DBGrid Sample
  29. ' The purpose of this sample is to give you an idea of what you have to do
  30. ' to get the DBGrid control working in unbound mode(DataMode = 1).
  31. ' For simplicity sake, this sample uses an array called UserData to store
  32. ' the data for the grid. In most cases, the DBGrid control can be bound
  33. ' to a Data control, however, some situations may require you to use it
  34. ' in unbound mode such as with a proprietary database format or for
  35. ' simple data sets for which there is no support by the data control.
  36. Private mTotalRows& ' Contains the total rows in the set of records
  37. Private UserData() As Variant ' 2-dimensional array containing records
  38. Private Const MAXCOLS = 3 ' Maximum number of fields in record set.
  39. ' When the user clicks on the Add icon, this subroutine adds a new
  40. ' row to the RowBuf variable and a bookmark to the NewRowBookmark
  41. ' variable
  42. Private Sub DBGrid1_UnboundAddData(ByVal RowBuf As RowBuffer, NewRowBookmark As Variant)
  43. Dim iCol As Integer
  44. mTotalRows = mTotalRows + 1
  45. ReDim Preserve UserData(MAXCOLS - 1, mTotalRows - 1)
  46. NewRowBookmark = mTotalRows - 1 'Sets the bookmark to the last row.
  47. ' The following loop adds a new record to the database.
  48. For iCol = 0 To UBound(UserData, 1)
  49.     If Not IsNull(RowBuf.Value(0, iCol)) Then
  50.         UserData(iCol, mTotalRows - 1) = RowBuf.Value(0, iCol)
  51.     Else
  52.         ' If no value set for column, then use the DefaultValue
  53.         UserData(iCol, mTotalRows - 1) = DBGrid1.Columns(iCol).DefaultValue
  54.     End If
  55. Next iCol
  56. End Sub
  57. ' This subroutine deletes a row based on it's bookmark.
  58. Private Sub DBGrid1_UnboundDeleteRow(Bookmark As Variant)
  59. Dim iCol As Integer, iRow As Integer
  60. ' Move all rows above the deleted row down in the
  61. ' array.
  62. For iRow = Bookmark + 1 To mTotalRows - 1
  63.     For iCol = 0 To MAXCOLS - 1
  64.         UserData(iCol, iRow - 1) = UserData(iCol, iRow)
  65.     Next iCol
  66. Next iRow
  67. mTotalRows = mTotalRows - 1
  68. End Sub
  69. ' This subroutine is called, any time the DBGrid wants to display
  70. ' new data.
  71. Private Sub DBGrid1_UnboundReadData(ByVal RowBuf As RowBuffer, StartLocation As Variant, ByVal ReadPriorRows As Boolean)
  72. Dim CurRow&, iRow As Integer, iCol As Integer, iRowsFetched As Integer, iIncr As Integer
  73. ' DBGrid is requesting rows so give them to it
  74. If ReadPriorRows Then
  75.     iIncr = -1
  76.     iIncr = 1
  77. End If
  78. ' If StartLocation is Null then start reading at the end
  79. ' or beginning of the data set.
  80. If IsNull(StartLocation) Then
  81.     If ReadPriorRows Then
  82.         CurRow& = RowBuf.RowCount - 1
  83.     Else
  84.         CurRow& = 0
  85.     End If
  86.     ' Find the position to start reading based on the
  87.     ' StartLocation bookmark and the iIncr variable
  88.     CurRow& = CLng(StartLocation) + iIncr
  89. End If
  90. ' Transfer data from our data set array to the RowBuf object
  91. ' which DBGrid uses to display the data
  92. For iRow = 0 To RowBuf.RowCount - 1
  93.     If CurRow& < 0 Or CurRow& >= mTotalRows& Then Exit For
  94.     For iCol = 0 To UBound(UserData, 1)
  95.         RowBuf.Value(iRow, iCol) = UserData(iCol, CurRow&)
  96.     Next iCol
  97.     ' Set bookmark using CurRow& which is also our
  98.     ' array index
  99.     RowBuf.Bookmark(iRow) = CStr(CurRow&)
  100.     CurRow& = CurRow& + iIncr
  101.     iRowsFetched = iRowsFetched + 1
  102. Next iRow
  103. RowBuf.RowCount = iRowsFetched
  104. End Sub
  105. ' This subroutine updates the data in the array after it has
  106. ' been edited.
  107. Private Sub DBGrid1_UnboundWriteData(ByVal RowBuf As RowBuffer, WriteLocation As Variant)
  108. Dim iCol As Integer
  109. ' Data is being updated
  110. ' Update each column in the data set array
  111. For iCol = 0 To MAXCOLS - 1
  112.     If Not IsNull(RowBuf.Value(0, iCol)) Then
  113.         UserData(iCol, WriteLocation) = RowBuf.Value(0, iCol)
  114.     End If
  115. Next iCol
  116. End Sub
  117. Private Sub Form_Load()
  118. ' 3 Columns, 15 Rows of Data
  119. ReDim UserData(0 To 2, 0 To 14)
  120. ' Row 1
  121. UserData(0, 0) = "Jack"
  122. UserData(1, 0) = "Handey"
  123. UserData(2, 0) = "19"
  124. ' Row 2
  125. UserData(0, 1) = "Bill"
  126. UserData(1, 1) = "Haney"
  127. UserData(2, 1) = "12"
  128. ' Row 3
  129. UserData(0, 2) = "Brent"
  130. UserData(1, 2) = "Lainge"
  131. UserData(2, 2) = "33"
  132. ' Row 4
  133. UserData(0, 3) = "Tom"
  134. UserData(1, 3) = "Bodett"
  135. UserData(2, 3) = "40"
  136. ' Row 5
  137. UserData(0, 4) = "Laura"
  138. UserData(1, 4) = "Schlessinger"
  139. UserData(2, 4) = "42"
  140. ' Row 6
  141. UserData(0, 5) = "Mike"
  142. UserData(1, 5) = "Lanzorotta"
  143. UserData(2, 5) = "28"
  144. ' Row 7
  145. UserData(0, 6) = "Brad"
  146. UserData(1, 6) = "Doer"
  147. UserData(2, 6) = "29"
  148. ' Row 8
  149. UserData(0, 7) = "Kevin"
  150. UserData(1, 7) = "Graves"
  151. UserData(2, 7) = "42"
  152. ' Row 9
  153. UserData(0, 8) = "Julie"
  154. UserData(1, 8) = "Madson"
  155. UserData(2, 8) = "31"
  156. ' Row 10
  157. UserData(0, 9) = "John"
  158. UserData(1, 9) = "Clark"
  159. UserData(2, 9) = "30"
  160. ' Row 11
  161. UserData(0, 10) = "Sidney"
  162. UserData(1, 10) = "Houghton"
  163. UserData(2, 10) = "28"
  164. ' Row 12
  165. UserData(0, 11) = "Bart"
  166. UserData(1, 11) = "Sampson"
  167. UserData(2, 11) = "60"
  168. ' Row 13
  169. UserData(0, 12) = "Greg"
  170. UserData(1, 12) = "Price"
  171. UserData(2, 12) = "33"
  172. ' Row 14
  173. UserData(0, 13) = "Bobby"
  174. UserData(1, 13) = "Carnahan"
  175. UserData(2, 13) = "30"
  176. ' Row 15
  177. UserData(0, 14) = "Iris"
  178. UserData(1, 14) = "Nedlemeyer"
  179. UserData(2, 14) = "60"
  180. mTotalRows& = 15
  181. Dim oldcnt As Integer, newcnt As Integer
  182. Me.Show
  183. oldcnt = DBGrid1.Columns.Count
  184. newcnt = 0
  185. Dim i As Integer
  186. ' Remove old columns
  187. For i = DBGrid1.Columns.Count - 1 To 0 Step -1
  188.     DBGrid1.Columns.Remove i
  189. Next i
  190. ' Add new columns
  191. For i = 0 To 2
  192.     DBGrid1.Columns.Add newcnt
  193.     DBGrid1.Columns(newcnt).Caption = "Col " & newcnt
  194.     DBGrid1.Columns(newcnt).Visible = True
  195.     newcnt = newcnt + 1
  196. Next i
  197. End Sub
  198.