Borland C++ Builder VCL Components >> TDBGrid

by Chris Luck » Thu, 04 Sep 2003 16:01:36 GMT

"Charlie Page" < XXXX@XXXXX.COM > wrote in message
news:3f527556$ XXXX@XXXXX.COM ...
> I can add a lookup field to a dataset and have a lookup field on the
> TDBGrid display a drop down list when the user clicks on it (default
> behavior).
> What I want is to have the list drop down when the user starts typing
> (have the box open without having to have the user use the the mouse to
> click). Is there any quick way to do this?

How quick is quick?

This is an adaptation of a scheme that I used to have activated by a MenuItem
with its Shortcut set to F4, that being the standard Windows key for dropping
ComboBoxes in general. The assumption here is that it is not required to
enter text into the edit box and only selections from the list are permitted.
That can be changed if required, also you may not want/need the test for
ButtonStyle as I do here. To achieve proper handling of the Return key I
found it necessary to Post a message and maintain a global flag to prevent
re-entrance. If you also use a KeyUp handler then it may be necessary to test
for "ListDropping" there too.

The keybd_event's emulate the Alt+DownArrow key-strokes that are normally used
to drop the list when the editor is showing, that being the easiest way to get
it to work. As shown it drops the list from Return, Space, F4 or any
character key. Escape works as standard and Return activates the selection
change in the usual way. The only minor defect is that the dropped list does
not handle the function keys so F4 can drop the list but cannot close it, that
could be fixed by adding a message trap. No doubt I'll hear about it if any
bugs show up. Done in BCB4.

.h file

class TMainForm : public TForm
__published: // IDE-managed Components
private: // User declarations
bool ListDropping;
void __fastcall CMDropPickList(TMessage &Message);
public: // User declarations
__fastcall TMainForm(TComponent* Owner);
extern PACKAGE TMainForm *MainForm;

cpp file

__fastcall TMainForm::TMainForm(TComponent* Owner)
: TForm(Owner)
ListDropping = false;

void __fastcall TMainForm::MyDBGridKeyDown(TObject *Sender, WORD &Key,
TShiftState Shift)
if (ListDropping)

int I = ((TDBGrid*)(Sender))->SelectedIndex;
if (!((TDBGrid*)(Sender))->Columns->Items[I]->ButtonStyle == cbsEllipsis)
if ((((TDBGrid*)(Sender))->Columns->Items[I]->PickList->Count > 0)
&& ((Key == VK_F4) || (Key == VK_RETURN) || (Key == VK_SPACE))
|| ((Key >= 'A') && (Key <= 'Z')))
if (!(IsWindowVisible
NULL), 0, "TPopupListBox",
PostMessage(Handle, CM_DROPPICKLIST, 0, 0);

void __fastcall TMainForm::MyDBGridKeyPress(TObject *Sender, char &Key)
int I = ((TDBGrid*)(Sender))->SelectedIndex;
if (!((TDBGrid*)(Sender))->Columns->Items[I]->ButtonStyle == cbsEllipsis)
if ((((TDBGrid*)(Sender))->Columns->Items[I]->PickList->Count > 0)
&& (Key != 13))
Key = 0;

void __fastcall TMainForm::CMDropPickList(TMessage &Message)
ListDropping = true;
if (ActiveControl->ClassNameIs("TDBGrid"))
if (!((TDBGrid*)(ActiveControl))->EditorMode)
((TDBGrid*)(ActiveControl))->EditorMode = true;

keybd_event(VK_MENU, 0, 0, 0);
keybd_event(VK_DOWN, 0, 0, 0);
keybd_event(VK_DOWN, 0, KEYEVENTF_KEYUP, 0);
keybd_event(VK_MENU, 0, KEYEVENTF_KEYUP, 0);
ListDropping = false;

Chris Luck.

Borland C++ Builder VCL Components >> TDBGrid

by FretBuzzz » Mon, 08 Sep 2003 05:14:55 GMT


Can anyone tell me if it's possible to change the color and font appearance
of a single row in a TDBGrid ?


Borland C++ Builder VCL Components >> TDBGrid

by Blue Knight » Wed, 21 Jan 2004 06:03:39 GMT

Has anyone a short example, that can be posted, on how to use TDBGrid?

Blue Knight

Borland C++ Builder VCL Components >> TDBGrid

by Oliver Young » Fri, 30 Jan 2004 22:29:00 GMT

How can I set font color (red, blue, green...) for every rows?

Thank you.

Borland C++ Builder VCL Components >> TDBGrid

by Don Locke » Wed, 07 Apr 2004 22:30:34 GMT

I have a TDBGrid that has some columns with numbers. I have it set up
so that when you press the space bar, it toggles the number from 0 to 1
or 1 to 0. This works OK. But I would like for it to then step to the
next column. Here if the code I have:

void __fastcall TBrochureForm::DBGrid1KeyPress(TObject *Sender, char &Key)
if(Key == VK_SPACE)
Toggle(); // Toggles the number - works OK
Key = 0;
if(DBGrid1->SelectedField == CustEditDM->CustListCURCAMP)
DBGrid1->SelectedField == CustEditDM->CustListCURCONT;
This does not step to the next field. I also tried:

if(Key == VK_SPACE)
DBGrid1KeyPress(this, VK_TAB);
This did not work either. Any ideas?

Borland C++ Builder VCL Components >> TDBGrid

by Don Locke » Wed, 07 Apr 2004 22:33:34 GMT

I don't know why my post ended up in this thread!! I did a compose &
not a reply.

Similar Threads

1. TDBGrid, I need help on it

> I am having trouble on inserting items into TDBGrid... I am getting
> from a txt file (integers and strings) and I need to insert this data
> into the grid to make a DB.

TDBGrid is not part of standard C++ hence your question is not topical
here. Your are more likely to get a good peer reviewed answer if you ask
this question on borland.public.cppbuilder.vcl.components.using (if your
newsserver doesn't carry this group, try the

2. Looking for a TDBGrid replacement or add-on. - Borland C++ Builder VCL Components

3. TDBGrid custom drawing ?


I want change dbgrid default drawing with this options:

- any row draw with defined colors, every other one
- selected row draw with defined color
- draw an picture on column title (for sorting ASC/DESC)

my code work, But when selected cell go editing focused text draw with 
default white on blue color, I want draw editing cell with defined color
how do it ?

- how can draw picture on column title ?

//////////// code
void __fastcall TGridSort::DBGridDrawColumnCell(TObject *Sender, const TRect 
&Rect, int DataCol, TColumn *Column, TGridDrawState State)
 TColor      clrRow;
 TFieldType  fType;
 TDBGrid   * dbGrid = static_cast <TDBGrid *> (Sender);

 if ( !dbGrid )

 fType  = Column->Field->DataType;
 clrRow = Column->Field->DataSet->RecNo % 2 ? FColorRow2 : FColorRow1;

 dbGrid->Canvas->Font->Color  = clBlack;
 if (State.Contains(gdSelected) || State.Contains(gdFocused))
  dbGrid->Canvas->Brush->Color = FColorRowSelect;
  dbGrid->Canvas->Font->Style  = TFontStyles() << fsBold;
 else if (dbGrid->SelectedRows->CurrentRowSelected) // If multiselect 
    dbGrid->Canvas->Brush->Color = FColorRowSelect;
  dbGrid->Canvas->Brush->Color = clrRow;
 dbGrid->DefaultDrawColumnCell(Rect, DataCol, Column, State);


4. MouseWheel AND TDBGrid - Borland C++ Builder VCL Components

5. TDBGrid left-right

How to move TDBGrid horizontaly with left-right key ? 
Now left-right=up-down.
What is the problem?

6. TDBGrid Question.

7. What property gives a TDBGrid's current row

Hi All

I know that the TDBGrid.SelectedIndex gives the current column in a TDBGrid
(when using Tab),
but what property (or method) may I use to get the current grid-row (from
the top of screen).  (I don't want to know the record... that is easy).


8. Question on TDBGrid. - Delphi/Pascal