DBGrid row count lazarus / 16-Databases ---------------------------------------------------------------------- yordan · 2011-01-26 10:11 Hi, I want to ask if there is a function that returns the number of rows in a DBGrid? If not please give me a hint how to count the rows in DBGrid.  Thank you!  Regards, Yordan ---------------------------------------------------------------------- yordan · 2011-01-26 11:09 OK I solved that one. If anyone is interested I used the sqlquery1.RecordCount (sqlquery1 is the query that provides the data for the DBGrid). But now I have a different question :   I want to select the row from DBGrid1 that has a value in the first column equal to a value entered in an edit box.  It should look something like this:     for i:=1 to (sqlquery1.RecordCount) do begin       if dbgrid1.Columns[0].Field.AsString = nomer_ed.Text then        dbgrid1.RowSelect;      end ;           But there isn't a function RowSelect.  Could you please help ---------------------------------------------------------------------- Mando · 2011-01-26 11:14 Hi, use this: Code: [Select] if dbgrid1.Columns[0].Field.AsString = nomer_ed.Text then    dbgrid1.SelectedRows.CurrentRowSelected:=true regards ---------------------------------------------------------------------- yordan · 2011-01-26 11:27 It doesn't work. The first row(which is selected by default when runnin the program) stays selected. ---------------------------------------------------------------------- Mando · 2011-01-26 11:36 You must move the active record: this a better way to do it: Code: [Select]  with dbgrid1.DataSource.DataSet do   begin      first;      while not eof do      begin         if dbgrid1.Columns[0].Field.AsString = nomer_ed.Text then           dbgrid1.SelectedRows.CurrentRowSelected:=true        next;      end ;           end; ---------------------------------------------------------------------- yordan · 2011-01-27 07:47 procedure TForm1.nomer_edKeyPress(Sender: TObject; var Key: char); begin    if Key = #13 then //Enter    begin   with dbgrid1.DataSource.DataSet do   begin     first;      while not eof do      begin         if dbgrid1.Columns[0].Field.AsString = nomer_ed.Text then           dbgrid1.SelectedRows.CurrentRowSelected:=true ;         next;      end ;   end;     I tried this code but still no luck now it selects only the last row from DBGrid1, no matter what I enter in the nomer_ed.Text  If someone has any other ideas why this is not working please help.  Thank you ---------------------------------------------------------------------- Mando · 2011-01-27 08:52 Hi Set dgMultiselect in Options of the dbGrid. Set dgPersistentMultiselect if you want to keep selected rows. if you allow me some advice: do not expect to find it all done ... must investigate and play with the controls. regards. ---------------------------------------------------------------------- yordan · 2011-01-27 09:24 Found it  procedure TForm1.Edit1KeyUp(Sender: TObject; var Key: Word; Shift: TShiftState); //var i: integer; begin  // i:=0;      if key=13 then    begin   //   with dbgrid1.DataSource.DataSet do  // begin    // first;      while not dbgrid1.DataSource.DataSet.EOF do      begin         if dbgrid1.Columns[1].Field.AsString = edit1.Text then          begin           dbgrid1.SelectedRows.CurrentRowSelected:=true ;           break;          end;         dbgrid1.DataSource.DataSet.Next;      end ;   end;   // end ; end; ---------------------------------------------------------------------- yordan · 2011-01-27 09:27 I wouldn't have done it without your help Mando thanks for the hint about dbgrid1.SelectedRows.CurrentRowSelected:=true I was stuck on this one for days ---------------------------------------------------------------------- Mando · 2011-01-27 10:53 Hi. your code only selects one record (the first match the condition). In addition, the first from the active record. To achieve this, is better use "locate". Code: [Select]procedure TForm1.Edit1KeyUp(Sender: TObject; var Key: Word; Shift: TShiftState); begin      if key=13 then        if dbgrid1.DataSource.DataSet(dbgrid1.Columns[1].Field.AsString ,edit1.Text) then dbgrid1.SelectedRows.CurrentRowSelected:=true ; end; regards