简体   繁体   中英

How to highlight data in clicked cell in a DBgrid (SMDBGrid)?

I'm using SMDBGrid in a Delphi application to show and edit data as as result of a query from MySQL.

I would like some help understanding how to make the grid behave the way I want when editing the data.

The data in the cells is initially shown in a black font on a white background.

  1. If I LEFT click once in a cell containg data, the cell changes to a white font on a white background (or maybe the data just vanishes) and I can't see the data to edit it.

  2. If I LEFT click once again in the same cell I get a white font on a blue background. I can see the data and I can edit it.

  3. If I LEFT click a third time in the same cell I get a black font on a white background (just like the other cells). I can see the data and I can edit it.

  4. DOUBLE clicking a cell once behaves like (1), double clicking thereafter always behaves like (2)

Question

How do I set it up so that left clicking once in a cell simply allows me to edit that cell without changing the colours? ie behave like in (3) above, continue showing a black font on whatever background colour the cell had orginally.

Other info that might be relevant

After running the query to populate the grid I put it into edit mode with

  • Query1.edit

I do have a OnMouseDown event in the Grid that detect a RIGHT click and show a PopUpMenu which offers choices and sets values in other fields of the record. There is also a OnGetCellParams event that sets the background colour of cells depending upon the value in these other fields. To get the popup menu to work properly and not show the (built in ?) editing menu when right clicked, by trial and error I set

  • PopUpMenu = MyPopUpMenu
  • dgEditing = true
  • dgAlwaysShowEditor = true
  • dgAlwaysShowSelection = false

but I'm not really sure what I am doing there.

Anyway, removing all that by removing the OnMouseDown and OnGetCellParams events and setting PopUpMenu to '' seemed to make no difference to the what happened when a cell was LEFT clicked. The data still went white.

I have found one way to do this now, but not sure if this is the correct/best way to do it as it seems I have to do by code something that I thought would have happened automatically. Anyway I got the behaviour by using the OnDrawColumnCell Event.

procedure TFrmDataEntry.SMDBGrid1DrawColumnCell(Sender: TObject;
  const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
//when they click in a cell, temporarily set the background to white and the font to bold black
if (gdSelected in State) then
    begin
    TDBGrid(Sender).Canvas.Brush.Color := clwhite;
    TDBGrid(Sender).Canvas.Font.Style := Font.Style + [fsBold];
    TDBGrid(Sender).Canvas.Font.Color := clblack;
    end;
 TDBGrid(Sender).DefaultDrawColumnCell(Rect, DataCol, Column, State); //Update the grid so it takes effect
 end; 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM