Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Checkbox in DBGrid using BCB5

974 views
Skip to first unread message

Mike I

unread,
Jun 23, 2008, 9:58:29 PM6/23/08
to
Hi,

I can't seem to find how to get to the C++ Builder area so I'm posting
here.

I've been trying to display a checkbox in a DBGrid (using ODBC to an MS
Access DB).

I've seen several examples using the OnDrawColumn event in Delphi but in
C++Builder, it never seems to trigger the State.Contains(gdFocused).
The checkbox appears correctly using DrawFrameControl but when I click
in the checkbox displayed in the DBGrid, the display reverts back to the
boolean state (I'm using True/False). Is there a way to have the
OnDrawColumn event detect when the selected boolean field is focused so
I can display a DBCheckbox properly?

Thanks in advance for any help.

Mike I

--- posted by geoForum on http://delphi.newswhat.com

Chris Luck

unread,
Jun 30, 2008, 2:20:57 AM6/30/08
to
Mike I wrote:
> I can't seem to find how to get to the C++ Builder area so I'm posting
> here.
> --- posted by geoForum on http://delphi.newswhat.com

The Borland (CodeGear) news server is: newsgoups.borland.com. Messages sent
via ISP mirrors of the newsgroups do not propagate to the Borland server.
If you can't use the newsgroups direct you can use their web interface -
http://newsgroups.borland.com

> I've been trying to display a checkbox in a DBGrid (using ODBC to an MS
> Access DB).
>
> I've seen several examples using the OnDrawColumn event in Delphi but in
> C++Builder, it never seems to trigger the State.Contains(gdFocused). The
> checkbox appears correctly using DrawFrameControl but when I click in the
> checkbox displayed in the DBGrid, the display reverts back to the
> boolean state (I'm using True/False). Is there a way to have the
> OnDrawColumn event detect when the selected boolean field is focused so I
> can display a DBCheckbox properly?


The following example was tested in BCB4. To prevent the editor showing
in a boolean field, whilst allowing other fields to be editable, requires
that the grid option 'dgEditing' be switched according to column field type.
'DefaultDrawing' is set false.

//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
// Create a DBGrid subclass to expose protected properties
class TAccessDBGrid : public TDBGrid
{
public:
__property DataLink;
__property Row;
__property FixedRows;
};
//---------------------------------------------------------------------------

void __fastcall TForm1::DBGrid1DrawColumnCell(TObject *Sender,
const TRect &Rect, int DataCol, TColumn *Column,
TGridDrawState State)
{
TRect NewRect = Rect;
bool DrawFocus = false;

// Cast as the grid subclass to access protected properties
if ((((TAccessDBGrid *)DBGrid1)->Row -
((TAccessDBGrid *)DBGrid1)->FixedRows ==
((TAccessDBGrid *)DBGrid1)->DataLink->ActiveRecord)
&& (State.Contains(gdFocused)))
{
DBGrid1->Canvas->Brush->Color = clHighlight;
DrawFocus = true;
}
else
{
DBGrid1->Canvas->Brush->Color = clWindow;
}
DBGrid1->Canvas->FillRect(Rect);


if (!(Column->Field->DataType == ftBoolean))
{
DBGrid1->DefaultDrawColumnCell(Rect, DataCol, Column, State);
}
else
{
if (Column->Field->AsBoolean)
{
DrawFrameControl(DBGrid1->Canvas->Handle, &NewRect,
DFC_BUTTON, DFCS_BUTTONCHECK | DFCS_CHECKED);
}
else
{
DrawFrameControl(DBGrid1->Canvas->Handle, &NewRect,
DFC_BUTTON, DFCS_BUTTONCHECK);
}
}
if (DrawFocus)
{
DBGrid1->Canvas->DrawFocusRect(Rect);
}
}
//---------------------------------------------------------------------------

void __fastcall TForm1::DBGrid1ColEnter(TObject *Sender)
{
// If the field is boolean, switch off Grid editing, else allow editing

if (DBGrid1->Columns->Items[DBGrid1->SelectedIndex]->Field->DataType
== ftBoolean)
{
DBGrid1->Options =
TDBGridOptions(dynamic_cast<TDBGrid *>(DBGrid1)->Options) >>
dgEditing;
}
else
{
DBGrid1->Options =
TDBGridOptions(dynamic_cast<TDBGrid *>(DBGrid1)->Options) <<
dgEditing;
}
}
//---------------------------------------------------------------------------

void __fastcall TForm1::DBGrid1Enter(TObject *Sender)
{
// Ensure that correct editing options are applied on
// first use. SelectedIndex defaults to the first column,
// if you tab into the grid or your first click goes to
// the first column then OnColEnter doesn't fire.
DBGrid1ColEnter(DBGrid1);
}
//---------------------------------------------------------------------------

void __fastcall TForm1::DBGrid1CellClick(TColumn *Column)
{
if (Column->Field->DataType == ftBoolean)
{
DBGrid1->DataSource->DataSet->Edit();
if (Column->Field->AsBoolean == true)
{
Column->Field->AsBoolean = false;
}
else
{
Column->Field->AsBoolean = true;
}
DBGrid1->DataSource->DataSet->Post();
}
}
//---------------------------------------------------------------------------

--
Regards,
Chris Luck

Mike I

unread,
Jul 9, 2008, 4:06:01 PM7/9/08
to
Chris,

Thanks for the reply and help. I added the code you posted and it works
pretty well.

There is a problem however when I scroll the DBGrid horizontally. If a
boolean field is focused, it appears correctly as a checkbox but after
scrolling, a number value (i.e. 113) appears in the DBGrid display.
Only after selecting another field does the checkbox re-appear
correctly.

Any ideas on how to correct this behavior?

Thanks again for your help.

Mike I

0 new messages