Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to listen to the new row event (ex.: with the down arrow) in a Grid? 1

Status
Not open for further replies.

Manuch

Programmer
May 6, 2022
16
IT
Hello,
I'm back working on VFP, I missed you dear people (and a little VFP too).

I just need to intercept the event when the user creates a new row in a grid, it can happen using the down arrow key, by mouse click and ways I don't think of right now.

thing_rnkqmu.png


I need to do this in order to automatically set some columns on a new row creation (so the user doesn't have to).
I tried with the Grid's AfterRowColChange event, it triggers at every cell change, but I'm not able to distinguish between rows (new, existing) because the only parameter that this event receives is the index of the cell (not the column or row) where the user moved. I wanted to keep track of the new/old rows myself in a cursor, but I'm not able to achieve this with the AfterRowColChange event, I did a few tests, since it triggers for columns and rows change and it only gives me the index of the cell (not of the row, that is what I need).

Can you please point me in the right direction?

Very thanks.
 
You have the row at any time, it's RECNO(). And ou have grid.activerow and grid.relativerow.

But in the bigger picture what you describe is default values. Make your DBF part of a DBC and you have that feature. I fear if you want to reinvent that wheel as your DBF needs to stay a free or fox2x table, then you don't get much empathy for insisting to reinvent what already exists. I'm sure there will be a way where you use a new DBF and then use it to feed to legaccy dbf for support of legacy code processing its data, and you cen get the best of both worlds without needing to tackle something hard at all.

Chriss
 
So far for the opinion part.

To point out why RECNO() tells you something: Do you use buffering? Well, then new records have RECNO()<0, that's your criterion for new rows.
You don't use buffering? Well then RECNO()=RECCOUNT() means you're at the last row.
Right, the second case is not telling whether that is newly created or you just scrolled to the last row that already existed.

You can remember which RECCOUNT() you had before. In each AfterRowColChange you can store that into a property of the grid or form. And at the next event you can check whether RECCOUNT() is the same as you stored into that property or has risen by one.

Can you really not come up with any of these ideas?

Chriss
 
Last not least. When you are at an old row, is it fair to assume those columns you want to set for new rows do already have values? So can you not do the most straight forward thing and check whether the columns you want to set to an automatic default value are empty/blank? That would really be the simplest, wouldn't it?

And then the fifth or sixth idea would be to add a field, maybe to any of your DBFs, that tells when the record was created and storing a datetime into it, when you first store it. Using default values that just means the default value is DATETIME(), not using defaults you recognize the field is the empty datetime and you set it to DETETIME() then. Once that's done, you can make it depending on that datetime vs current DATETIME(), whether you recognize the state of the record as old and fixed or new and ready to set some columns.

Chriss
 
Sorry Chris for taking your time with such a dumb question, and very thanks for reminding me, I had completely forgot about RECNO.

I didn't work on VFP for some months, I forgot everything, thank you very much, also for providing me a simple way (that yes, I could have tought of myself) to distinguish between rows. I had a big part of this same work that I did in a very messy way because I didn't remember about RECNO, that I'll have to re-write. Sorry again and very thanks, have a good day / afternoon (there it's 7 pm).
 
Well, there are some cases you'll miss anyway.

If code adds records, i.e. the reason for the new rrecord does not originate from a user action, the rowcolchange event won't happen, but there still is one new row.
Which means an INSERT or APPEND does not make the grid move to that record immediately, though activating the grid then will mean it becomes the active grid row, too.

I don't recommend letting the grid event add the data into a record, for that reason. At least not as the only way.

And also look into the grid property AllowAddNew. If you disallow that, the user will only scroll to the last row and not add a new one. So you gain more control. You can offer an Add button and then don't just APPEND BLANK but INSERT INTO the table including default values, if not using the DEFAULT in your table definition, inclding to call stored procs that determine a value, not just simple defaujlts like DATE() or constant values.

And then you can react to the keypress event, which can check if the key used was downarrow and the row is RECCOUNT() currently. And then add the row with default data right away.

Also maybe you'd like to use another mechanism like SET CARRY, see the help.

Chriss
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top