DBGrid. Limiting the number of rows lazarus / 31-LCL ---------------------------------------------------------------------- iru · 2015-03-21 04:48 Gentlefolk, I am developing a small application to input data from track and field events at athletics meetings. For sprint events there is generally 8-10 lanes per heat. I have a small test application with a DBGrid-Datasource-SQLQuery- FireBird structure which displays 8 rows in the DBGrid. The DBGrid is configured with Autoadvance = aaDown. My problem is how to stop the the DBGrid adding another row when I enter data and press 'Enter' while the current 'cell' is in row 8.  I have read a lot and tried various things like setting "Key" to $0 like the following code sample: In the procedure "TFM.DBGrid1KeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);" I have some code like  If TDBGrid(Sender).DataSource.DataSet.RecNo = 8 then   Begin     Key := $0;     // TDBGrid(Sender).DataSource.DataSet.Prior; // This resulted in setting the row to 7 when enabled.   end;    I always wind up in the 'current' cell in a new row 9. Any suggestions, anyone? Ian ---------------------------------------------------------------------- BitBangerUSA · 2015-03-21 04:59 perhaps turn off DBGrid's auto advance when cell #8 is active/reached/clicked in... ---------------------------------------------------------------------- DonAlfredo · 2015-03-21 06:06 Try this, by putting the code below into the dataset AfterInsert event ! Code: [Select]if (DataSet.EOF) AND (NOT DataSet.BOF) then DataSet.Cancel; ---------------------------------------------------------------------- iru · 2015-03-21 06:33 BitBangerUSA, Tried "perhaps turn off DBGrid's auto advance when cell #8 is active/reached/clicked in"  By Setting DBGrid.AutoAdvance := aaNone when reaching row 8. I still wind up creating row "9" with the active cell in another column. I tried  setting "aaNone" when in row7. Worked, sort of, active cell was in row 8 but in another column. I have tried to manage the column with the following code. No luck still wind up in another column. Thank you, Ian.   Begin     // Key := $0; // Either enabled or disabled has no effect.     TDBGrid(Sender).AutoAdvance:= aaNone;     TDBGrid(Sender).SelectedIndex := 1;  // No effect either here before the AutoAdvance. ---------------------------------------------------------------------- iru · 2015-03-21 06:41 DonAlfredo, Thank you for your response. It works. Everything stopped in row 8, even in the correct cell. As usual I think my problem is that I think the DBGrid controls things when actually it is the underlying dataset! Thank you both for responding, Ian. ---------------------------------------------------------------------- iru · 2015-03-21 07:01 Gentlefolk, I have done some more testing and things are not as good as I thought. The solution proposed by DonAlfredo certainly stops adding an additional row but there is then a problem with the "edit" state of the cell. The correct cell is highlighted but the cell is not in "edit" mode so I cannot enter or edit any data unless I click on the cell with the mouse. Any more ideas??? Thanks, Ian ---------------------------------------------------------------------- iru · 2015-03-21 09:30 Gentlefolk, Taking things a little further..... I have added a "Dataset.Edit" as follows: procedure TFM.SQLQueryHeatAfterInsert(DataSet: TDataSet); begin   // From DonAlfredo in the LAZ LCL forum. Works, stops everything in the last row   if (DataSet.EOF) AND (NOT DataSet.BOF) then   Begin     DataSet.Cancel;     DataSet.Edit; // <<<<<<<<<<<<<<<<<<<<<<<<<<<<   Add this   end; end;          The cell in row 8 is now in edit mode. Ian ---------------------------------------------------------------------- BitBangerUSA · 2015-03-21 15:55 well, duh me. yeppers that the dataset is the place to handle the problem... i must remember to think before reacting... glad your code now works.