You are on page 1of 4

FlexGrid as DataGrid

Introduction
I create a new AciveX control (FDGrid) to let FlexGrid control as DataGrid control. With this ActiveX, you can:

Connect with database Fill FlexGrid with table Edit and add records MoveFirst, MoveLast, MoveNext and MovePrevious Auto save (Update) after Edit cell Edit grid without connecting to database file

Background
When using my ActiveX control (FDGrid), if you use it as DataGrid you must load the grid with data from the database file. See the code in the form (Form1). You can edit the grid without loading data. See the code in the form (Form2). Some properties and methods of my ActiveX are in the following table: Method/Property
DatabaseName DataSource AddNewRow DeleteRow Update CancelUpdate MoveFirst MoveLast MoveNext

Definition Set database name Set Recordset to grid Add new record to grid Delete record Save record after add Cancel update Move to first record Move to last record Move to next record

Example
FDGrid1.DatabaseName = FileName FDGrid1.DataSource = rs FDGrid1.AddNewRow FDGrid1.DeleteRow FDGrid1.Update FDGrid1.CancelUpdate FDGrid1.MoveFirst FDGrid1.MoveLast FDGrid1.MoveNext

Method/Property
MovePrevious

Definition Move to previous record

Example
FDGrid1.MovePrevious

Other Properties and Methods as (MSFlexGrid) control. To use my ActiveX (FDGrid), you must add only one file from files: "Microsoft DAO 3.51 (or 3.6) Object Library" to the reference and add (FDGrid) control to the Toolbox.

Using the Code Form1 (connect to the database file and use ActiveX as MSDataGrid)
LoadData()

procedure:

Collapse
Dim MyDataFile As String Dim MyDb As Database Dim MySql As String MyDataFile = App.Path + "\DataFile\" + "Sale.mdb" Set MyDb = OpenDatabase(MyDataFile, False, False, ";pwd=" & "") MySql = "SELECT * FROM Products Order by ProductID" FDGrid1.DatabaseName = MyDataFile FDGrid1.RecordSource = MySql FDGrid1.RecordsetType = vbRSTypeDynaset FDGrid1.Refresh Form_Load()

procedure:

'Call LoadData procedure LoadData cmdAdd_Click()

Collapse

procedure:

' add new record to data base file FDGrid1.AddNewRow cmdCancel_Click()

Collapse

procedure:

' don't save record FDGrid1.AddNewRow cmdDelete_Click()

Collapse

procedure:

' delete record from data base file

Collapse

FDGrid1.DeleteRow cmdUpdate_Click()

procedure:

' save record to data base file FDGrid1.Update

Collapse

Refer to Form1 to see code for MoveFirst, MoveLast, MoveNext and MovePrevious.

Form2 (use ActiveX to edit grid)


LoadData()

procedure:

FDGrid1.Cols = 8 'grid has 8 columns FDGrid1.Rows = 15 'grid has 15 rows 'set alignment of fixed row to center For c = 1 To FDGrid1.Cols - 1 FDGrid1.TextMatrix(0, c) = "Col " & CStr(c) FDGrid1.ColAlignmentHeader(c) = flexAlignCenterCenter Next c ' fill some rows For r = 1 To 5 For c = 1 To FDGrid1.Cols - 1 FDGrid1.TextMatrix(r, c) = "Cell(" & CStr(r) & "," & CStr(c) & ")" Next c Next r ' You can edit any cell, just click any cell then try to edit it.

Collapse

Remarks
When extracting the FDGrid.zip file, you can find the FDGrid.ocx file in the ActiveXcontrol folder. Find the database file Sale.mdb in the DataFile folder. Find the prjBoundFlex project (to test the ActiveX control) in the FDGrid folder. This project has three forms (frmMain, Form1 and Form2).
Form1 Form2

is to connect grid with database file. is to edit grid.

Last Words
I hope this article is useful and helps you when your application needs to connect with the database or needs to edit grid. Please tell me if you have any ideas or if you find any problems. Thanks to Code Project and thanks to all.

You might also like