Click here to Skip to main content
15,878,970 members
Articles / Multimedia / GDI

Transfer Data from MSFlexGrid to Graph Chart

Rate me:
Please Sign up or sign in to vote.
4.50/5 (2 votes)
30 May 2008CPOL2 min read 45K   1.6K   23   3
View one row numeric data of MSFlexGrid as Graph Chart
FlexToGraph

Introduction

I wrote an article earlier about ActiveX control (FDGrid) to allow FlexGrid control as a DataGrid control. To see this ActiveX and its application, click here.

Now I create a new AciveX control (MKGraph) to transfer data from FlexGrid to a graph chart:

  • Connect with database
  • Fill FlexGrid with table
  • Add or Edit records
  • Choose numeric columns
  • Choose graph type
  • Edit title and legend
  • Click to graph
  • Save graph chart
  • Copy graph chart

Background

When using my ActiveX control (MKGraph), you must load the grid with data from the database file, see the code in the form (frmMain). Some properties and methods of my ActiveX are in the following table:

Method/Property Definition Example
DatabaseNameSet database nameMKGraph1.DatabaseName = FileName
DataSourceSet the Recordset to gridMKGraph1.DataSource = rs
AddNewRowAdd new record to gridMKGraph1.AddNewRow
DeleteRowDelete recordMKGraph1.DeleteRow
UpdateSave record after addMKGraph1.Update
CancelUpdateCancel updateMKGraph1.CancelUpdate
MoveFirstMove to first recordMKGraph1.MoveFirst
MoveLastMove to last recordMKGraph1.MoveLast
MoveNextMove to next recordMKGraph1.MoveNext
MovePreviousMove to previous recordMKGraph1.MovePrevious
DataToGraphTell ActiveX which columns to drawMKGraph1.DataToGraph
DrawGraphBegin to graph (three steps)MKGraph1.DrawGraph

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

Using the Code

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"
   MKGraph1.DatabaseName = MyDataFile
   MKGraph1.RecordSource = MySql
   MKGraph1.RecordsetType = vbRSTypeDynaset
   MKGraph1.Refresh

cmdLoad_Click() 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

cmdDraw_Click() procedure:

VB.NET
' Tell ActiveX control which data to draw
MKGraph1.DataToGraph

' Begin to graph: choose type of graph, edit title and legend, view graph.
MKGraph1.DrawGraph

When you click cmdDraw button, the ActiveX control will graph with three steps:

  • Choose the graph type (when you see the form which has the caption "Graph type": click any graph type).
  • Edit the title and legend of graph (when you see the form which has the caption "Graph data").
  • Graph chart (now you see your graph on the final form).

Remarks

When extracting the file FlexToGraph.zip, you can find the file MKGraph.ocx in the folder ActiveXcontrol.

Find database file Sale.mdb in the DataFile folder.

Find the project prjFlexGraph (to test the ActiveX control) in the folder FlexToGraph.

This project has only one form (Form1).

This form has:

  • Button to load data from database file
  • Button to graph
  • Button to add record
  • Button to delete record
  • Button to update (save record)
  • Button to cancel update
  • Four buttons for MoveFirst, MoveLast, MoveNext and MovePrevious
  • Button to exit

Last Words

I hope this article is useful and helps you to graph some data in your applications. Please tell me if you have any ideas or if you find any problems. Thanks to CodeProject and thanks to all.

History

  • 30th 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

 
JokeSuperb Pin
zero.zero26-May-10 2:01
zero.zero26-May-10 2:01 
GeneralRe: Superb Pin
Mostafa Kaisoun26-May-10 14:13
Mostafa Kaisoun26-May-10 14:13 
GeneralTo hide my name and email. Pin
Mostafa Kaisoun31-May-08 13:19
Mostafa Kaisoun31-May-08 13:19 

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.