Topic: Row select and focus after save ?

I have Form1 with a tGrid and Form2 for adding record to tGrid on Form1.


I cannot get the saved record selected and focused on Form1 tGrid after clicking a save button on Form2 with the script below:

procedure Form1_OnShow (Sender: TObject; Action: string);
begin
  form1.TableGrid1.selectedrow := 0;
  form1.TableGrid1.setfocus;
end;

procedure Form2_btnDetSave_OnAfterClick (Sender: TObject);
begin
   Form1.TableGrid1.SelectedRow := Last_Insert_id;
   form1.TableGrid1.setfocus;
end;

procedure Form1_btnAdd_OnAfterClick (Sender: TObject);
begin
   Form1.TableGrid1.SelectedRow := Last_Insert_id;
   form1.TableGrid1.setfocus;
end;

procedure Form1_btnEdit_OnAfterClick (Sender: TObject);
begin
  form1.TableGrid1.setfocus;
end;

procedure Form1_btnDelete_OnAfterClick (Sender: TObject);
begin
  form1.TableGrid1.setfocus;
end;

.

Could Dmitry or somebody tell me what's wrong with the script please?

Adam
God... please help me become the person my dog thinks I am.

2 (edited by mathmathou 2017-12-22 21:03:34)

Re: Row select and focus after save ?

Hello my friend,


Has strange as it may seem, you don(t need to use SelectedRow. It i used to find the ID or index of the selected row, not to set it.


Use a simple :

procedure keywords_Button8_OnClick (Sender: string; var Cancel: boolean);
var
    lid : Integer;
begin
    lid := Last_Insert_id;
    keywords.TableGrid1.dbItemID := lid;
end;

And you will get the focus on the last inserted row in the Tablegrid (my procedure is on a button click, but should work at the end of any procedure)


Merry Christmas


Cheers


Mathias

I'm a very good housekeeper !
Each time I get a divorce, I keep the house

Zaza Gabor

Re: Row select and focus after save ?

Hi Mathias,


Nice one...
Thank you very much..................
Truly appreciated........................
You wouldn't believe how long I spent on this trying all kind of things with selectedRow and scrollToRow.... Now, I can go and get a drink thanks to you Math.


I used it like this:

var lid : Integer;
// TG 1
procedure Form1_btnLIDtg1_OnClick (Sender: TObject; var Cancel: boolean);
begin
  lid := Last_Insert_id;
  form1.TableGrid1.dbItemID := lid;
  form1.TableGrid1.SetFocus;
end;

procedure Form2_btnDetSave_OnAfterClick (Sender: TObject);
begin
  form1.btnLIDtg1.Click;
end;

// TG 2
procedure Form1_btnLIDtg2_OnClick (Sender: TObject; var Cancel: boolean);
begin
  lid := Last_Insert_id;
  form1.TableGrid2.dbItemID := lid;
  form1.TableGrid2.SetFocus;
end;

procedure Form3_Button1_OnAfterClick (Sender: TObject);
begin
  form1.btnLIDtg2.Click;
end;

Var is declared as global since I have more than one tGrid and used hidden add button.


Merry Christmas................

Adam
God... please help me become the person my dog thinks I am.

4 (edited by derek 2017-12-23 00:04:38)

Re: Row select and focus after save ?

Hi Adam, Hi Mathias (it's been a while - how are you?),
Yes, I can believe how long you spent on it - it's quite awkward.
It is not so difficult to focus on a record in the tablegrid that has just been inserted by using 'last_insert_id', BUT I think this only works if the tablegrid is unsorted and the new record is therefore always inserted at the bottom of the grid.
But when the tablegrid has a sort sequence, then the newly inserted record can be anywhere in the tablegrid - that's the more awkward bit.
The attached manages to work around that problem but one problem remains that I can't fix.
Here is the scenario -
1.  tablegrid is sorted by name
2.  a record is amended and, for example, the name (ie the field that the tablegrid is sorted on!) is changed from 'Mathias' to 'Adam'.
3.  when the record is saved, the incorrect row is now highlighted ('Mathias' has moved (because of the sort sequence) but the highlight remains on the same tablegrid auto-increment row no' as before).
Any ideas how to work around this (something like a 'last_change_id' - but it doesn't exist - LOL!).
Regards,
Derek.

Post's attachments

Attachment icon place on new row.zip 344.85 kb, 412 downloads since 2017-12-23 

5 (edited by derek 2017-12-23 17:34:33)

Re: Row select and focus after save ?

Hi Adam, Mathias,
Update on my last post.
Attached is a new version that (I think) gets round the problem of the incorrect record being highlighted when the sorted field is changed.
So, in summary,
1. a saved (inserted) row is highlighted when you go back to the grid.
2. an amended row is correctly highlighted when you go back to the grid (even if the sort order has been changed).
3. a deleted row moves the cursor to the next row in the grid.
Hope this helps and you can follow what the script is doing - pretty messy and there are probably more efficient ways to do it.
Regards,
Derek.

Edit:  just noticed a line of code that shouldn't have been there - taken out and re-uploaded 23.12.2017 @ 17.31 GMT.  Sorry about that

Post's attachments

Attachment icon place on new row.zip 345.74 kb, 421 downloads since 2017-12-23 

Re: Row select and focus after save ?

Hi Derek, Mathias,


Thanks for the latest workaround Derek.....
As usual nothing can escape from you.


I was mainly trying to apply after save row selecting and focus for treeview simulation. Derek, I started with your cities example applying Math's method as it seemed to be simpler approach. It works with grid1 fine but doesn't with grid 2 and 3. Could it be the reason that grid 2 and 3 retrieves data via search action? Is there solution for this? I couldn't get it working. I don't need grid sorting via grid settings.


Also, after delete I wanted next selected record display it's details (in this case review memo content) but couldn't. Tried adding cellclick for delete button but couldn't make it work?


Please see attached sample project:

Post's attachments

Attachment icon TreeViewSim NS with Scroll.zip 13.27 kb, 406 downloads since 2017-12-23 

Adam
God... please help me become the person my dog thinks I am.

Re: Row select and focus after save ?

Hi Adam,
Totally agree that it's more straightforward if you ignore any pre-sort settings on the grid - I just wanted to find a way around it just in case (and it was a useful learning exercise, as always).
I'll try and have a look at your latest attachment.
Derek.

Re: Row select and focus after save ?

Hello guys,


@Derek : you're right, I've been on and off for a few month because work was quite heavy lately, but I always keep an eye on the forum and on old friends smile


Three things I wanted to add :


First, 'keywords.TableGrid1.dbItemID := lid;' refers to a database ID, so should not be "broken" by any sorting. The tablegrid I tested it on was sorted and it still worked.
But then, this 'Last_Insert_id' thing only works on a database INSERT. If you try this method on an UPDATE, it won't give you the last modified ID, but the last inserted one, which could have been added hours ago...
If you want to select the row after an UPDATE, you'll have to identify the database ID with another method first.


Second, you can not set the focus on multiple objects on a form. So if you want to highlight lines in different grid, don't use the 'setfocus' command.
I personally setup my grids with a 'selectfullrow' option and this is enough to highlight the row I want.
More on that after Christmas if needed.


Last but not least, I see a lot of attempt at Treeviews, so I thought I might post my implementation of it here :

My application is an asset manager, assets to which you can attach keywords to identify them. They also have a link to the online shop you can find them and a status flag (owned, wanted, download in progress...).


The following treeview represents the assets grouped by Keywords, and the vendors with icons for each levels and a special icon to show their status.

You will see :
first level : keywords (this is test data)
second level : vendors
third level : assets

Each level has it's own icon, and some assets (third level) have a little green tick, showing they are owned. And each level also has a counter showing how many items it contains.


https://i.imgur.com/7YRkQNc.png


I've stripped down all the rest of the code, all other forms and all irrelevant data from the database because the package was over 300Mo wether now, it's a little under 1,5 Mo.


Hope this helps


Have yourself a Merry Christmas as used to sing Franck Sinatra.


Cheers


Mathias

Post's attachments

Attachment icon DRAMA2.1-Icons.zip 1.43 mb, 530 downloads since 2017-12-23 

I'm a very good housekeeper !
Each time I get a divorce, I keep the house

Zaza Gabor

Re: Row select and focus after save ?

Hi Mathias,


Very nice treeview you have created. Thank you very much for sharing................ Truly appreciated..................
Compare to you, I'm a nob. Would you be so kind to demonstrate with a basic application how to put it in use?
You don't need to include "Backup" folder as you already have included it in your upload above.


Hi Derek,


As I try to do more with MVD's current tGrid, I come across more flaws of it.
I found out that, issue of not selecting last saved record was happening when tGrid set to use child records. It may also be same with tGrids populated via search.
Also, if text wrapping, padding script included for the tGrid same issue appears again.
It'd be great if you can find a workaround about these issues without breaking our tGrids chain.

Adam
God... please help me become the person my dog thinks I am.