Click here to Skip to main content
15,885,546 members
Articles / Desktop Programming / Win32

FlexGrid as DataGrid

Rate me:
Please Sign up or sign in to vote.
2.50/5 (2 votes)
12 May 2008CPOL2 min read 76.4K   2K   15   9
ActiveX control makes FlexGrid as DataGrid and edits FlexGrid
FDGrid

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 Definition Example
DatabaseNameSet database nameFDGrid1.DatabaseName = FileName
DataSourceSet Recordset to gridFDGrid1.DataSource = rs
AddNewRowAdd new record to gridFDGrid1.AddNewRow
DeleteRowDelete recordFDGrid1.DeleteRow
UpdateSave record after addFDGrid1.Update
CancelUpdateCancel updateFDGrid1.CancelUpdate
MoveFirstMove to first recordFDGrid1.MoveFirst
MoveLastMove to last recordFDGrid1.MoveLast
MoveNextMove to next recordFDGrid1.MoveNext
MovePreviousMove to previous recordFDGrid1.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:

VB.NET
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:

VB.NET
'Call LoadData procedure
LoadData

cmdAdd_Click() procedure:

VB.NET
' add new record to data base file
FDGrid1.AddNewRow

cmdCancel_Click() procedure:

VB.NET
' don't save record
FDGrid1.AddNewRow

cmdDelete_Click() procedure:

VB.NET
' delete record from data base file
FDGrid1.DeleteRow

cmdUpdate_Click() procedure:

VB.NET
' save record to data base file
FDGrid1.Update

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

Form2 (use ActiveX to edit grid)

LoadData() procedure:

VB.NET
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.

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 is to connect grid with database file.
  • Form2 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.

History

  • 12th May, 2008: Initial post

Mostafa Kaisoun
M_Kaisoun@hotmail.com

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Egypt Egypt
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questionhow to use FDGrid.ocx in vs2003? Pin
unbelieable25-Oct-12 18:05
unbelieable25-Oct-12 18:05 
AnswerRe: how to use FDGrid.ocx in vs2003? Pin
Mostafa Kaisoun26-Oct-12 5:09
Mostafa Kaisoun26-Oct-12 5:09 
Generalfdgrid control in vb.net2003 Pin
Mostafa Kaisoun7-Sep-08 17:22
Mostafa Kaisoun7-Sep-08 17:22 
Questionfdgrid usage in vb.net2003 Pin
Member 45877696-Sep-08 21:10
Member 45877696-Sep-08 21:10 
GeneralFDGrid uses DAO Pin
Mostafa Kaisoun5-Sep-08 8:41
Mostafa Kaisoun5-Sep-08 8:41 
Dear Boris,
As you know, the FlexGrid not support ADO, it is work with DAO control.
To use FDGrid:
1- Convert your data base file *.mdb to Access 97 from MSAccess menu:
Tools: Database Utilities: Convert Database: To Access97 File Format.
2- Link the FDGrid with data base file and RecordSource as you see in the procedure (LoadData) in the Form1 which you found in my article.

Waiting you for result.
Thanks,
Mostafa kaisoun
GeneralRe: FDGrid uses DAO Pin
vocilos11-Sep-08 6:29
vocilos11-Sep-08 6:29 
QuestionFDGrid Pin
vocilos5-Sep-08 4:53
vocilos5-Sep-08 4:53 
GeneralTo hide my name and email [modified] Pin
Mostafa Kaisoun31-May-08 13:15
Mostafa Kaisoun31-May-08 13:15 
GeneralHelp! Pin
Mostafa Kaisoun12-May-08 13:09
Mostafa Kaisoun12-May-08 13:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.