• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 739
  • Last Modified:

Print DBGrid

Hi guys,

I'm trying to print a DBGrid using C++ Builder 6.

I've thought about moving the data from a DBGrid to a Memo box, and then printing that, but I can't get the data from the DBGrid successfully (can get number of Columns + Rows using dsStock->DataSet->RecordCount and dsStock->DataSet->FieldCount, but cannot access the specific cell data)

Any ideas? The simplier the better please :)

  • 5
  • 4
1 Solution
I don't know C++ Builder, DBGrid and Borland Memo box, but I would assume that you have to extract the grid value of each cell and put it to a formatted text string:

#include <string>
#include <sstream>
#include <iomanip>
using namespace std;

string gridToMemo(DBGrid* dsStock)
   ostringstream osmemo;
   for (int r = 0; r < dsStock->DataSet->RecordCount; ++r)
        for (int c = 0; c <  dsStock->DataSet->FieldCount; ++c)
              string cell = dsStock->DataSet->GetValue(r, c);   // Don't know the name of
                                                                                     // the member function to extract
                                                                                     // a string value from data cell
              int width = dsStock->DataSet->GetColWidth(c); // Don't know the name of
                                                                                    // the function to get the maximum
                                                                                    // width of a column
              osmemo << setw(width+1) << cell;
        osmemo << endl;

    return osmemo.str();

Regards, Alex
adamcableAuthor Commented:

Thanks for this.

Unfortunately your comments are part of my problem too - I don't know how to extract cell data from the DBGrid.

Any ideas would be gratefully received!

>>>> I don't know how to extract cell data

Check class definition of DBGrid for functions named 'GetData', 'GetValue', 'GetCellData', 'value' or post the class definition here. I am absolutely sure there is a function.

Regards, Alex

Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

adamcableAuthor Commented:
I think I'll need a helping hand here ;)

The best I can find is a GetFieldData, but that only returns a bool.

How do I get the class definition?

>>>> GetFieldData

doesn't sound bad. The bool return is ok as the function could return the value via argument (reference or pointer type). I would assume there are various GetFieldData functions for the different type, maybe defined similar to

   bool GetFieldData( int row, int c, char* buf, int bufSiz);

Please post the prototypes of the function(s). You could find them in the class definition which should be found in a header file named 'dbgrid.h' or similar. It should be in an subdirectory 'include' or 'inc' of the installation path of C++ Builder. You could make a grep on *.h and "GetFieldData" to locate the header.

Regards, Alex
adamcableAuthor Commented:
In dbtables.hpp, I found the following declarations:

      __fastcall virtual TBDEDataSet(Classes::TComponent* AOwner);
      __fastcall virtual ~TBDEDataSet(void);
      void __fastcall ApplyUpdates(void);
      virtual bool __fastcall BookmarkValid(void * Bookmark);
      virtual void __fastcall Cancel(void);
      void __fastcall CancelUpdates(void);
      __property bool CacheBlobs = {read=FCacheBlobs, write=FCacheBlobs, default=1};
      virtual int __fastcall CompareBookmarks(void * Bookmark1, void * Bookmark2);
      void __fastcall CommitUpdates(void);
      Word __stdcall ConstraintCallBack(Bde::DsInfoReq Req, Bde::DataSources &ADataSources);
      bool __fastcall ConstraintsDisabled(void);
      virtual Classes::TStream* __fastcall CreateBlobStream(Db::TField* Field, Db::TBlobStreamMode Mode);
      void __fastcall DisableConstraints(void);
      void __fastcall EnableConstraints(void);
      void __fastcall FetchAll(void);
      void __fastcall FlushBuffers(void);
      virtual bool __fastcall GetCurrentRecord(char * Buffer);
      virtual int __fastcall GetBlobFieldData(int FieldNo, Db::TBlobByteData &Buffer);
      virtual bool __fastcall GetFieldData(Db::TField* Field, void * Buffer)/* overload */;
      virtual bool __fastcall GetFieldData(int FieldNo, void * Buffer)/* overload */;
      void __fastcall GetIndexInfo(void);
      virtual bool __fastcall Locate(const AnsiString KeyFields, const Variant &KeyValues, Db::TLocateOptions Options);
      virtual Variant __fastcall Lookup(const AnsiString KeyFields, const Variant &KeyValues, const AnsiString ResultFields);
      virtual bool __fastcall IsSequenced(void);
      virtual void __fastcall Post(void);
      void __fastcall RevertRecord(void);
      virtual Db::TUpdateStatus __fastcall UpdateStatus(void);
      virtual int __fastcall Translate(char * Src, char * Dest, bool ToOem);
      __property bool ExpIndex = {read=FExpIndex, nodefault};
      __property Bde::hDBICur Handle = {read=FHandle};
      __property Word KeySize = {read=FKeySize, nodefault};
      __property void * Locale = {read=FLocale};
      __property TDataSetUpdateObject* UpdateObject = {read=FUpdateObject, write=SetUpdateObject};
      __property bool UpdatesPending = {read=GetUpdatesPending, nodefault};
      __property Db::TUpdateRecordTypes UpdateRecordTypes = {read=GetUpdateRecordSet, write=SetUpdateRecordSet, nodefault};
>>>> __fastcall virtual TBDEDataSet(Classes::TComponent* AOwner);

What is the classname of the class above?

I would assume it isn't DBGrid but TBDEDataSet cause that looks like a constructor (though 'virtual' constructors are invalid in C++).

We need the class definition of DBGrid. Could you make a grep for DBGrid?

>>>> virtual bool __fastcall GetFieldData(Db::TField* Field, void * Buffer)/* overload */;

That function gives the value of a record currently fetched from database. So, actually it doesn't help us as the data are already fetched and stored into a grid.

Regards, Alex

adamcableAuthor Commented:
Got it working :)

TDataSet* pDS = dbGridStock->DataSource->DataSet;

//Select first item in list

//Loop round rows
for (int i=0; i< dsStock->DataSet->RecordCount; i++) {

//Loop round columns and get data
for (int j = 0; j < pDS->FieldCount; j++)

 s = s + pDS->Fields->Fields[j]->AsString;
 s = s+", ";

//Select the next item in the list

Regards, Alex

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now