Recent

Author Topic: DBGrid row count  (Read 23002 times)

yordan

  • New Member
  • *
  • Posts: 18
DBGrid row count
« on: January 26, 2011, 11:11:48 am »
 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

  • New Member
  • *
  • Posts: 18
Re: DBGrid row count
« Reply #1 on: January 26, 2011, 12:09:52 pm »
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 O:-)
 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

  • Full Member
  • ***
  • Posts: 181
Re: DBGrid row count
« Reply #2 on: January 26, 2011, 12:14:54 pm »
Hi, use this:

Code: [Select]
if dbgrid1.Columns[0].Field.AsString = nomer_ed.Text then
   dbgrid1.SelectedRows.CurrentRowSelected:=true


regards

yordan

  • New Member
  • *
  • Posts: 18
Re: DBGrid row count
« Reply #3 on: January 26, 2011, 12:27:06 pm »
It doesn't work. The first row(which is selected by default when runnin the program) stays selected.  :(

Mando

  • Full Member
  • ***
  • Posts: 181
Re: DBGrid row count
« Reply #4 on: January 26, 2011, 12:36:18 pm »
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;
« Last Edit: January 26, 2011, 12:40:15 pm by Mando »

yordan

  • New Member
  • *
  • Posts: 18
Re: DBGrid row count
« Reply #5 on: January 27, 2011, 08:47:10 am »
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

  • Full Member
  • ***
  • Posts: 181
Re: DBGrid row count
« Reply #6 on: January 27, 2011, 09:52:23 am »
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

  • New Member
  • *
  • Posts: 18
Re: DBGrid row count
« Reply #7 on: January 27, 2011, 10:24:03 am »
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

  • New Member
  • *
  • Posts: 18
Re: DBGrid row count
« Reply #8 on: January 27, 2011, 10:27:35 am »
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

  • Full Member
  • ***
  • Posts: 181
Re: DBGrid row count
« Reply #9 on: January 27, 2011, 11:53:22 am »
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

 

TinyPortal © 2005-2018