Mouse over dbgrid's title - delphi

Is there a way to capture name of title with mouse move over Dbgrids title?
Idea is to make title name visible only when cursor is moved over title's field.
Tnx in advance.

The code below show how to get the "title" of the grid column the mouse pointer is over.
Actually, what is displayed in the column header of a TDBGrid column is the Caption property of the column's Title object, which has other properties too. The code below reads and displays the Caption property of the Title.
As you can see from the online help, the TColumn objects which are the columns of the grid also have Field and FieldName properties which you can read if required.
procedure TForm1.DBGrid1MouseMove(Sender: TObject; Shift: TShiftState; X, Y:
Integer);
var
Col,
Row : Integer;
begin
Col := DBGrid1.MouseCoord(X, Y).X;
Row := DBGrid1.MouseCoord(X, Y).Y;
Caption := Format('Col: %d, Row:%d', [Col, Row]);
if (Col > 0) and (Col <= DBGrid1.Columns.Count) then
Caption := Caption + DBGrid1.Columns[Col - 1].Title.Caption;
end;

To answer the question, what you need to know here is
1: Which cell the mouse is over (and hence whether it is over a title Cell)
and
2: the field name (title).
Both these are possible, but not sure how you would use this information to make the title name visible.
1: is to trap the OnMouseMove event and use the MouseCoord property.
2: is to use the resultant column value (if the Row value is 0) and the Fields[ACol].FieldName property.
But perhaps a more direct way to achieve what you want is to set dgTitleHotTrack in the options and set a hottrack style (which would probably have to be a custom one).

Related

Delphi: Bind a Chart with a DBGrid

I want to bind a Chart with a DBGrid.
Refer to the exhibit.
When I click in the DBGrid on number 3 (X-Axis), then the bar at position three should be highlighted(or a pointer to bar 3).
When I click on number 4 in the Grid then bar 4 is highlighted etc.
I'm using a TDBChart
Is there a way to do this?
Not knowing what the charting component is, cannot provide a working example, but the key which you are looking for is to use the AfterScroll event for the dataset in the grid. Since each row represents a different record, that event is fired when the grid selection moves to the row.
Edit: This doesn't highlight with a box, but does change the color of the value marks at the top of each bar. Hopefully this gets you on your way. MyQuery is what is feeding the datasource
var
savecolor : tcolor;
procedure MyForm.FormShow(Sender:TObject);
begin
...
SaveColor := dbchart1.series[0].marks.items[0].color;
...
end;
procedure MyForm.MyQueryBeforeScroll(DataSet : TDataSet);
begin
dbchart1.series[0].marks.items[MyQuery.recno-1].color := SaveColor;
end;
procedure MyForm.MyQueryAfterScroll(DataSet:TDataSet);
begin
dbchart1.series[0].marks.items[MyQuery.recno-1].color := clRed;
end;

Delphi, Show button in a different TGridPanelLayout cell

Hi I am working with XE6 and I am using a TGridPanelLayout with 4 col and 4 rows. On the first cell I am displaying a Button. What I would like to do is, that when I click on this Button, get that Button to appear in a different cell. But I cannot find how to do it, so far I tried this, but nothing happens.
procedure TForm4.Button1Click(Sender: TObject);
begin
GridMyPannel.ControlCollection.BeginUpdate;
GridMyPannel.ControlCollection.AddControl(Button1, 2, 2);
Button1.Parent := GridMyPannel;
end;
I am really new on Delphi. Could anyone give me an example of how I could do it?
A TGridPanel has a ControlCollection property which allows access to the Row and Column properties that also appear on your TButton once you've placed in inside your TGridpanel. A TButton (or rather its superclass TControl) does not have a Row or Column property. So we need to get a grip of the TControlItem wrapper the TGridpanel uses.
procedure TForm8.Button1Click(Sender: TObject);
var
selectedControl: TControl;
itemIndex: Integer;
selectedControlItem: TControlItem; // This knows about "Row" and "Column"
begin
// This is the button we've clicked
selectedControl := Sender as TControl;
itemIndex := GridPanel1.ControlCollection.IndexOf(selectedControl);
if (itemIndex <> -1) then begin
selectedControlItem := GridPanel1.ControlCollection.Items[itemIndex];
selectedControlItem.Row := Random(GridPanel1.RowCollection.Count);
selectedControlItem.Column := Random(GridPanel1.ColumnCollection.Count);
end;
end;
The above code finds the button and changes its Row and Column property to a random value. Note that you didn't specify whether the TButton is the only control within the TGridpanel. Is that the case?
I did the below in normal VCL and XE3 and with a TGridPanel (no TGridPanelLayout in my Delphi).
The problem with the GridPanel is that it does not allow controls (Buttons, etc) to be placed in any cell (like Cell:1,1) without having controls in the cells before that cell. GridPanel always fills itself from Index 0 upwards.
So the trick is to fool it. Now depending on whether you already have other cells in the GridPanel you will have to make place for the button to go to and also put something else in its place if the button was in a cell of lower index.
Have a look at the form before the button is pressed:
Note that I have not created a ControlItem at cell 1,0 yet.
I want to move Button 1 to cell 1,0. I cannot do that unless I first place something else in its place (cell 0,0). I have to create a new ControlItem at cell 1,0 to house button1.
procedure TForm1.Button1Click(Sender: TObject);
begin
// Places CheckBox1 in the same cell as BUtton1
GridPanel1.ControlCollection.ControlItems[0,0].Control := CheckBox1;
// Create a new ControlItem for Button1 and in the same breath move
// Button1 to it
GridPanel1.ControlCollection.AddControl(Button1,1,0);
// You know what this does. :)
CheckBox1.Parent := GridPanel1;
end;
The result:

Delphi Listview leaves selection marks behind [duplicate]

I am using listview in vsreport virtual mode, have two columns and after I populate the list, select one row, and then select some other row there's an artifact left on a previously selected row, see image below. How to fix this?
and this is my code that gets the data from an array
procedure TForm1.ListView1Data(Sender: TObject; Item: TListItem);
begin
Item.Caption := FloatToStr(Item.Index + 1);
Item.SubItems.Add(FloatToStr(a[Item.Index]));
end;
When I hover the mouse, the artifacts dissapear.
Thanks
Setting the listview's DoubleBuffered property to true solves the artifacts problem.

hyperlink field in DBGRID

I'm doing a small internal software to search branch lines in my company. In addition to the branch lines I also put an e-mail field in the database as shown below:
My intention is to click on the registered e-mail and the software via the ShellExecute open a window to send the e-mail. I'm using the option dgRowSelect as TRUE and because of that the OnCellClick event does not correctly identify which cell was clicked.
In my searches have not found any way to do this. Then I thought of using a TLabel within the field. I can use the onclick event in the TLabel and also change the cursor icon.
If TLabel is a good solution, how can I add a TLabel in DBGrid?
Or what would be another good solution?
I'm guessing that purpose of dgRowSelect=true is for highlighting whole selected row.
TLabel is not the way I would go - I would set dgRowSelect=false and paint selected row in OnDrawColumnCell or create my own fixed dbgrid.
There was a similar question:
how can i colour whole row in DBGrid with rowselect turned off?
Anyway if you want to use dgRowSelect=true and get valid info about clicked cell, here it is:
type THackDBGrid=class(TDBGrid);
procedure TForm1.dbgrd1CellClick(Column: TColumn);
var p:TPoint;
col:TGridCoord;
i: Integer;
grid:THackDBGrid;
begin
p := Mouse.CursorPos;
grid := THackDBGrid(Column.Grid);
p := grid.ScreenToClient(p);
col := grid.MouseCoord(p.X,p.Y);
i := grid.RawToDataColumn(col.X);
Label1.Caption := 'Column index: ' + IntToStr(i);
end;

DBGrid get selected cell

I need to get the value of the selected cell of a DBGrid in Delphi.
I have no idea how to do it. I tried dbGrid's OnMouseMove
pt : TGridCoord;
...
pt:=dbGrid.MouseCoord(x, y);
[Edited]
I can use the OnCellClick to get the value of the cell with "Column.Field.AsString", but I want to get the value from the first column when I click on any column of that row.
Found it.
dbGrid.Fields[0].AsString gets the value of the first column of the selected row.
procedure TForm1.DBGrid_DrawColumnCell(Sender: TObject; const Rect: TRect; DataCol: Integer; Column: TColumnEh; State: TGridDrawState);
const defaultCheckBoxFieldNumber = 1;
begin
if DBGrid.SelectedField.FieldNo = defaultCheckBoxFieldNumber then
....;
else
...;
end;
DBGrid.SelectedField.FieldNo gets selected field on event DrawColumnCell in TDBGrid.
I know this is late and not sure if it is what the title means.
But if it means to get the selected cell value, then try this:
procedure Form1.dbGrid1CellClick(Column: TColumn);
begin
ShowMessage(table1.Fields[Column.Index].AsString);
end;
Make sure
dbGrid1.Options.dbRowSelect := False;
i think the easiest way is to connect a hidden DBText to your dataset then set the DBText to display which field you need, this way that DBText will always contain the needed value of the active record
A DBGrid has no focus, and therefore you cannot find out which row is seleted. Instead look at the linked DataSet. A DataSet has an active row.
try this to get the value of selected cell in dbgrid:
procedure Form1.dbGrid1CellClick(Column: TColumn);
begin
ShowMessage(table1.Fields[DBGrid1.SelectedIndex].AsString);
end;

Resources