Crash Course Delphi 18 [beta]:
CSV Files - part 5


Add and delete records

Adding records

Analysis:

  1. Simply add a row to the stringgrid.
  2. Make the cells of that new row empty.
  3. Position the stringgrid on the new row. Now, the user can edit the new record.

Deleting records

Analysis:

1. If the stringgrid is not focused, then stop processing (exit the procedure). Because in this case, it's not clear as to which row must be deleted.

2. Delete or blank the row: start by saving the current row number. Next:

2.1 If there are more than 2 rows in the stringgrid (the fixed top row plus at least 2 rows with data), then...

2.1.1 If there are rows after the current row, then...

2.1.1.1 Shift all these rows up 1 position. Next, go to step 2.1.2

2.1.2 Delete the last row from the stringgrid. In code: decrement its RowCount by 1.

2.2 But if there are only 2 rows left, the fixed top row plus 1 row with data, then "blank" (empty) the cells of the current row.
Reason: deleting the row would leave us with only the fixed row with the headings. And that's impossible, because the FixedRows property of a stringgrid always must be less then its RowCount. (If you don't believe me, just try it ;)



A better way to edit records: see next lessons. To be continued...

« Lesson 17