Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleUse an unbound DBGrid control
KeywordsDBGrid, unbound, unbound data
CategoriesControls, Database
 
This example uses the following data structure to hold data.
 
Private Type BookInfo
    Title As String
    ISBN As String
    URL As String
End Type
Private MaxBook As Integer
Private BookInfos() As BookInfo
 
When the program starts, it calls subroutine LoadData to load some data from a file into the BookInfos array.

When the program unloads, it calls SaveData to write the data back into the file.

 
' Load the data from a file.
Private Sub LoadData()
Dim fnum As Integer
Dim fname As String
Dim i As Integer

    fnum = FreeFile
    fname = App.Path & "\books.txt"
    Open fname For Input As #fnum

    Input #fnum, MaxBook
    ReDim BookInfos(0 To MaxBook)

    For i = 0 To MaxBook
        With BookInfos(i)
            Input #fnum, .Title, .ISBN, .URL
        End With
    Next i

    Close #fnum
End Sub

' Save the data into a file.
Private Sub SaveData()
Dim fnum As Integer
Dim fname As String
Dim i As Integer

    fnum = FreeFile
    fname = App.Path & "\books.txt"
    Open fname For Output As #fnum

    Write #fnum, MaxBook

    For i = 0 To MaxBook
        With BookInfos(i)
            Write #fnum, .Title, .ISBN, .URL
        End With
    Next i

    Close #fnum
End Sub
 
The program must provide four event handlers for the DBGrid control. UnboundReadData loads data into the control beginning at the location StartLocation.
 
' Load data into the control.
Private Sub DBGrid1_UnboundReadData(ByVal RowBuf As _
    MSDBGrid.RowBuffer, StartLocation As Variant, ByVal _
    ReadPriorRows As Boolean)
Dim dr As Integer
Dim row_num As Integer
Dim r As Integer
Dim rows_returned As Integer

    ' See which direction to read.
    If ReadPriorRows Then
        dr = -1
    Else
        dr = 1
    End If

    ' See if StartLocation is Null.
    If IsNull(StartLocation) Then
        ' Read from the end or beginning of
        ' the data.
        If ReadPriorRows Then
            ' Read backwards from the end.
            row_num = RowBuf.RowCount - 1
        Else
            ' Read from the beginning.
            row_num = 0
        End If
    Else
        ' See where to start reading.
        row_num = CLng(StartLocation) + dr
    End If
    
    ' Copy data from the array into RowBuf.
    rows_returned = 0
    For r = 0 To RowBuf.RowCount - 1
        ' Do not run beyond the end of the data.
        If row_num < 0 Or row_num > MaxBook Then Exit For

        ' Copy the data into the row buffer.
        With BookInfos(row_num)
            RowBuf.Value(r, 0) = .Title
            RowBuf.Value(r, 1) = .ISBN
            RowBuf.Value(r, 2) = .URL
        End With

        ' Use row_num as the bookmark.
        RowBuf.Bookmark(r) = row_num

        row_num = row_num + dr
        rows_returned = rows_returned + 1
    Next r

    ' Set the number of rows returned.
    RowBuf.RowCount = rows_returned
End Sub
 
UnboundWriteData saves data modified by the control into the array.
 
' Save data updated by the control.
Private Sub DBGrid1_UnboundWriteData(ByVal RowBuf As _
    MSDBGrid.RowBuffer, WriteLocation As Variant)
    ' Update only the values that have changed.
    With BookInfos(CInt(WriteLocation))
        If Not IsNull(RowBuf.Value(0, 0)) Then _
            .Title = RowBuf.Value(0, 0)
        If Not IsNull(RowBuf.Value(0, 1)) Then _
            .ISBN = RowBuf.Value(0, 1)
        If Not IsNull(RowBuf.Value(0, 2)) Then _
            .URL = RowBuf.Value(0, 2)
    End With
End Sub
 
UnboundDeleteRow removes a specified row from the array. This program uses an entry's index in the array as its bookmark so it's easy to tell which row is being deleted.
 
' Delete an entry.
Private Sub DBGrid1_UnboundDeleteRow(Bookmark As Variant)
Dim r As Integer

    ' Fill in the hole.
    For r = Bookmark + 1 To MaxBook
        BookInfos(r - 1) = BookInfos(r)
    Next r

    MaxBook = MaxBook - 1
End Sub
 
Finally, UnboundAddData adds a new entry to the array. Notice that the event handler sets the new bookmark (index in the array) for output.
 
' Add a new entry.
Private Sub DBGrid1_UnboundAddData(ByVal RowBuf As _
    MSDBGrid.RowBuffer, NewRowBookmark As Variant)
    ' Make room for the new entry.
    MaxBook = MaxBook + 1
    ReDim Preserve BookInfos(0 To MaxBook)

    ' Set the bookmark to the new entry.
    NewRowBookmark = MaxBook

    ' Save the new data.
    With BookInfos(MaxBook)
        If Not IsNull(RowBuf.Value(0, 0)) Then
            .Title = RowBuf.Value(0, 0)
        Else
            .Title = DBGrid1.Columns(0).DefaultValue
        End If
        If Not IsNull(RowBuf.Value(0, 1)) Then
            .ISBN = RowBuf.Value(0, 1)
        Else
            .ISBN = DBGrid1.Columns(1).DefaultValue
        End If
        If Not IsNull(RowBuf.Value(0, 2)) Then
            .URL = RowBuf.Value(0, 2)
        Else
            .URL = DBGrid1.Columns(2).DefaultValue
        End If
    End With
End Sub
 
To add a new row programmatically, the code updates the data array and calls the control's Refresh method to make it reload its data.
 
' Add a new record.
Private Sub cmdAdd_Click()
    MaxBook = MaxBook + 1
    ReDim Preserve BookInfos(0 To MaxBook)
    With BookInfos(MaxBook)
        .Title = txtTitle.Text
        .ISBN = txtISBN.Text
        .URL = txtURL.Text
    End With

    DBGrid1.Refresh
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated