More Delphi 7 DBCtrlGrid Legacy tricks - lookup

 09 08 06

posted earlier on dbctrlgrid's paintpanel event to show/hide (draw/notdraw) components.

Today, I further developed a workaround for the dbctrlgrid, because I needed a lookup combobox (dblookupcombobox) on the grid.

If you place one normally, you cannot assign the ListSource design-time. Logically, you cannot then assign KeyField or ListField. ("Operation not allowedDBCtrlGrid"). Trying to override this run-time may be possible, but I haven't tried.

Adding a lookupfield field to the underlying dataset (adoquery in my case) also does not help (in my case) since the key is an ID (int) looking up a string (person name). The lookup field will not allow correct assigning, neither with a combobox or a dblookupcombobox.

The trick is, again, to use the painpanel event, this time to draw a control (the lookup control in this case) that was placed outside the dbctrlgrid on the correct spot in the dbctrlgrid. You can use the Index in-event variable against the panelindex property to make sure you only "draw" (position) in the selected canvas. You may need a dummy version of the dynamically draw-positioned control on the non-selected rows / panels. (Depends on your application).

The paintpanel code:

 

procedure TFPVA2.DBCtrlGrid1PaintPanel(DBCtrlGrid: TDBCtrlGrid;

  Index: Integer);

begin

  try

    DBCtrlGrid1.Canvas.Lock;

    if index = dbctrlgrid1.PanelIndex then  begin

      dblookupcombobox1.Top:=DBCombobox2.Top+ dbctrlgrid1.Top+dbctrlgrid1.PanelIndex*dbctrlgrid1.PanelHeight;

      dblookupcombobox1.Left:=65;

    end;

    DBCtrlGrid1.Canvas.Unlock;

  except end;

end;

The Canvas.Lock and Unlock may not be needed for your app. The 'try except end' statement was used because Delphi throws "Canvas does not allow drawing" errors that actually do not block the code or it's functionality.

 

 

Comments

Powered by BlogEngine.NET 1.5.0.7 - Old School Theme by n3o Web Designers &
Tobias op den Brouw.