• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 721
  • 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 :)

Thanks,
Adam
0
adamcable
Asked:
adamcable
  • 5
  • 4
1 Solution
 
itsmeandnobodyelseCommented:
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
0
 
adamcableAuthor Commented:
Alex,

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!

Thanks,
Adam
0
 
itsmeandnobodyelseCommented:
>>>> 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
 

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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?

Thanks,
Adam
0
 
itsmeandnobodyelseCommented:
>>>> 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
0
 
adamcableAuthor Commented:
In dbtables.hpp, I found the following declarations:

public:
      __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};
      
Adam
0
 
itsmeandnobodyelseCommented:
>>>> __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





0
 
adamcableAuthor Commented:
Got it working :)

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

//Select first item in list
pDS->First();

//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
pDS->Next();
}
0
 
itsmeandnobodyelseCommented:
Congratulations.

Regards, Alex

0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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