Finding column selected in Delphi grid

Norm-al
Norm-al used Ask the Experts™
on
Hi experts,

I have a double-click event in a Delphi grid which is opening a data entry form, but I need a way of determining which column the cell that was clicked resides in so that variable can be passed to the DEF.

I'm assuming this can be done fairly easily and probably in a few different ways; if possible, I would greatly appreciate being able to see a few different examples.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
Try something like this...

YourVar:=DBGrid1.SelectedField.FieldName;

Open in new window

Norm-alNetwork Engineer

Author

Commented:
I've been looking for an option like that but can't seem to find any sort of 'SelectedField' property under the grid...

The closest I've found is DBGrid1.Columns.FocusedCellViewInfo... but I'm not sure if I'm headed down the right path here.

Commented:
What's your Delphi version?
Acronis in Gartner 2019 MQ for datacenter backup

It is an honor to be featured in Gartner 2019 Magic Quadrant for Datacenter Backup and Recovery Solutions. Gartner’s MQ sets a high standard and earning a place on their grid is a great affirmation that Acronis is delivering on our mission to protect all data, apps, and systems.

Norm-alNetwork Engineer

Author

Commented:
Ah sorry, probably should have posted that to begin with:

CodeGear Delphi 2007, version 11.0.2902.10471
Ephraim WangoyaSoftware Engineer

Commented:

What grid component are you using?
Norm-alNetwork Engineer

Author

Commented:
A devExpress tcxGrid.
Ephraim WangoyaSoftware Engineer

Commented:

 TcxGridDBColumn(ACellViewInfo.Item).DataBinding.FieldName

Commented:
How About this?

cxGrid1.Controller.FocusedColumn
Commented:
or maybe this for the index number...

cxGrid1.Controller.FocusedColumnIndex 

Open in new window

Norm-alNetwork Engineer

Author

Commented:
Ah, perfect!

cxGrid1.Controller.FocusedColumn.Name was exactly what I was looking for. I'm not sure why I didn't think to look under the .Controller property.

Going by the column index would also have worked, however the way that I am using referencing the variable from the DEF would mean that if the column were moved prior to it opening the index would be wrong.


ewangoya, your method of referencing the field name would also work just as well, but I can't seem to get it to work--it hits that line and errors out. What sort of simpleness am I screwing up? :(

Commented:
Glad it helped.
Ephraim WangoyaSoftware Engineer

Commented:

I'm not sure why you could not get it to work, the item proprty references the column on the grid and you can typecast it to a DB Column

MyField := TcxGridDBColumn(ACellViewInfo.Item).DataBinding.FieldName;

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial