How to create(Add) Fixed Columns in TDBGrid

This is the forum for miscellaneous technical/programming questions.

Moderator: 2ffat

How to create(Add) Fixed Columns in TDBGrid

Postby minas » Sun Aug 08, 2004 7:17 pm

Hello everybody,

is it possible to create fixed Columns in a DBGrid?

I tryed DBGrid1->FixedCols =2 but is not accessible so I used a casting

like TStringGrid(DBGrid1).FixedCols=2 but doesn't work.

Thanks in advance
"Only the virtue's conquests have certainty"
Sofokleous Erephyle
User avatar
minas
BCBJ Guru
BCBJ Guru
 
Posts: 183
Joined: Sat Jul 10, 2004 6:09 am
Location: Crete,Greece

Postby minas » Mon Aug 09, 2004 9:40 am

Ok resolved ! By declaring a new Class derivated from TDBGrid with public FixedCols :
//**************************************
class TNewDBGrid : public TDBGrid
{
typedef TDBGrid inherited;
private: // User declarations
protected:
public: // User declarations
__property FixedCols;
};

//************************

in .cpp

i use
reinterpret_cast<TNewDBGrid*>(DBGrid1)->FixedCols=2; //if Options contains dgIndicator
else i use
reinterpret_cast<TNewDBGrid*>(DBGrid1)->FixedCols=1;

And the big problem is to add row numbers in that fixed column.

I use

void __fastcall TForm1::DBGrid1DrawColumnCell(TObject *Sender,
const TRect &Rect, int DataCol, TColumn *Column,
TGridDrawState State)
{
if (State.Contains(gdFixed) && DataCol==0){
int rowH= Rect.Height();
int row_num = (Rect.Bottom-rowH)/rowH;
DBGrid1->Canvas->Brush->Color = clBtnFace;
DBGrid1->Canvas->FillRect(Rect);
DBGrid1->Canvas->Font->Color = clBlack;
int w = DBGrid1->Canvas->TextWidth(IntToStr(row_num));
int lpos =Rect.left+( Rect.Width()/2 - (w/2) );
DBGrid1->Canvas->TextOutA(lpos,Rect.top,IntToStr(row_num));
}
else
DBGrid1->DefaultDrawColumnCell(Rect,DataCol,Column,State);
}

when scroll down the first line gets always value 1 and this is not correct if there are more records than lines in DBGrid.
I cannot use TQuery->RecNo to indentify the recNo because is -1.
How to do it !!!
Can anyboby help ?

Thanks in advance.
"Only the virtue's conquests have certainty"
Sofokleous Erephyle
User avatar
minas
BCBJ Guru
BCBJ Guru
 
Posts: 183
Joined: Sat Jul 10, 2004 6:09 am
Location: Crete,Greece

A small correction

Postby minas » Mon Aug 09, 2004 6:36 pm

The class must be

class TNewDBGrid : public TDBGrid
{
typedef TDBGrid inherited;
private: // User declarations
protected:
public: // User declarations
__property FixedCols;
__property DefaultRowHeight;
};


and OnDrawColumnCell use :

if (State.Contains(gdFixed) && DataCol==0){
int rowH= reinterpret_cast<TNewDBGrid*>(DBGrid1)-DefaultRowHeight;
int row_num = Rect.top/ rowH;
...
}


Can anybody help , How to add correctly row numbers in Fixed Column?
The problem is when I scroll the DBGrid.
Thanks
"Only the virtue's conquests have certainty"
Sofokleous Erephyle
User avatar
minas
BCBJ Guru
BCBJ Guru
 
Posts: 183
Joined: Sat Jul 10, 2004 6:09 am
Location: Crete,Greece


Return to Technical

Who is online

Users browsing this forum: TurnitinBot [Bot], Yahoo [Bot] and 8 guests