kirank
July 24th, 1999, 02:43 AM
Hi all ,
I want to get and set the cells of DBgrid control
DBGrid control I dont want to bound to any record set , But want to populate the cells with my own data , Can I do the same , If not which grid control can I use .
Dr_Michael
July 24th, 1999, 08:18 AM
My friend, after adding a dbgrid on your form (i named it dbgrid2) add this code and don't forget to right click on the dbgrid, select properties and choose UNBound mode! I use an array called HY, where i pass the values (don't forget to change the value of MTotalRows). After that the dbgrid display the data and also i can change the data and add some new or even to delete a row. Any problems? Just email me... :-)
' for simplicity sake, i use an array called HY to store
' the data for the grid.
private Const MAXCOLS = 9 ' 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 DBGrid2_UnboundAddData(byval RowBuf as RowBuffer, NewRowBookmark as Variant)
Dim iCol as Integer
mTotalRows = mTotalRows + 1
ReDim Preserve HY(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(HY, 1)
If Not IsNull(RowBuf.Value(0, iCol)) then
HY(iCol, mTotalRows - 1) = RowBuf.Value(0, iCol)
else
' If no value set for column, then use the DefaultValue
HY(iCol, mTotalRows - 1) = DBGrid2.Columns(iCol).DefaultValue
End If
next iCol
End Sub
' This subroutine deletes a row based on it's bookmark.
private Sub DBGrid2_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
HY(iCol, iRow - 1) = HY(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 DBGrid2_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
else
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
else
' 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(HY, 1)
RowBuf.Value(iRow, iCol) = HY(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 DBGrid2_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
HY(iCol, WriteLocation) = RowBuf.Value(0, iCol)
End If
next iCol
End Sub
private Sub Form_Load()
' 9 Columns, 10 Rows of Data
ReDim HY(0 to 8, 0 to 9)
'run test
'column 0
HY(0, 0) = 0.5
HY(0, 1) = 1
HY(0, 2) = 2
HY(0, 3) = 5
HY(0, 4) = 14
HY(0, 5) = 30
HY(0, 6) = 60
HY(0, 7) = 120
HY(0, 8) = 480
HY(0, 9) = 1440
'column 1
Dim j as Integer
for j = 0 to 9
HY(1, j) = 21
next j
'column 2
HY(2, 0) = 36
HY(2, 1) = 32.5
HY(2, 2) = 29
HY(2, 3) = 24
HY(2, 4) = 20.2
HY(2, 5) = 17.3
HY(2, 6) = 15
HY(2, 7) = 13.2
HY(2, 8) = 10.7
HY(2, 9) = 9
'end of run test
mTotalRows& = 10
Dim oldcnt as Integer, newcnt as Integer
me.Show
oldcnt = DBGrid2.Columns.Count
newcnt = 0
Dim i as Integer
' Remove old columns
for i = DBGrid2.Columns.Count - 1 to 0 step -1
DBGrid2.Columns.Remove i
next i
' Add new columns
for i = 0 to 2
DBGrid2.Columns.Add newcnt
DBGrid2.Columns(newcnt).Caption = "Col " & newcnt
DBGrid2.Columns(newcnt).Visible = true
newcnt = newcnt + 1
next i
End Sub
Michael Vlastos
Company MODUS SA
Development Department
Athens, Greece
Tel: +3-01-9414900