1
votes

I'd like to identify the screen coordinates of the Title cell which was clicked on the TDBGrid event TitleClick(Column: TColumn).

I can use the ColWidths property (exposed via a TDBGrid = class(DBGrids.TDBGrid) type declaration) but I am having difficulty in determining if the columns have been re-ordered by the user, combined with horizontal scrolling of the TDBGrid. I'd also like to keep track of where this Column is during subsequent moves and resizings, noting also that it's possible this column can be scrolled off the grid.

I've spent a long while on this problem and I am rather good at Delphi so this is not an idle question.

2
Why should you worry if the column is off grid? Obviously the column is visible if a user can click on the title, right?Hendra
I'm using the Title click to change indexes, and I'm showing a little bitmap next to that column to indicate which the current sort order is. If the grid is sorted in an order which is on a field that isn't currently visible then I don't want to see the image.Jonathan
There are free components that can do this , such as smdbgrid component from (scalabium.com), and JvDbgrid from (jvcl.delphi-jedi.org).Hendra
I'm implementing a complex TDBGrid which will have TEdit's floating below each column heading where the user can enter filters. Later that'll be extended to a customed TComboBox where multiple filters and ranges can be entered against each column, so I am building my own component, but it's all on the fly as its going into a TFrame which technically makes the TFrame itself my complete component. With that level of customisation its easier to work off a simpler base, besides I'm not going to use a custom component for what you solved for me in 1 line of code (row=0 as I'm looking at the Title!)Jonathan
Ahh, i see, now I understand. The custom components I mentioned above, of course, not just having sorting feature, but many more. My intention mentioning them was you might want to explore their features, and only implement additional ones which not implemented yet, so you don't have to start from scratch.Hendra

2 Answers

4
votes

Using the trick in How do I get screen coordinates of the DBGrid cell, I wrote:

type
...
  THackedGrid = class(TDBGrid);
...
implementation
...
procedure TForm1.DBGrid1TitleClick(Column: TColumn);
var
  currRow : Integer;
  rect : TRect;
begin
  currRow := THackedGrid(DBGrid1).Row;
  rect := THackedGrid(DBGrid1).CellRect(Column.Index+1,currRow);
end;

Is this what you want? The coordinate in rect is relative to the grid.

0
votes

I started working on a very similar grid yesterday at work. I am overlaying a control on the grid fixed row as you mentioned, activating it on a right click. This is what I doing so far, then setting the filter on my dataset. I have issues however when using multiselect on the combo. I would be very interested in seeing what you have accomplished since the last post.

procedure Tf_well.dbWellGridMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
var cell  : TGridCoord;
begin
    if Button = mbRight then
    begin
        Cell := dbWellGrid.MouseCoord(X, Y);
        //  showmessage(dbWellGrid.Columns[Cell.X-1].DisplayName);

        case Cell.X-1 of
            0:  begin
                    fieldComboWellName.Visible:=True;
                    fieldComboWellName.DroppedDown:=True;
                    fieldComboWellName.SetFocus;
                end;