* * *

Author Topic: How can get scroll position of DBGrid ?  (Read 1417 times)

majid.ebru

  • Full Member
  • ***
  • Posts: 200
How can get scroll position of DBGrid ?
« on: June 12, 2017, 11:33:05 am »
Hi
i put a DBGrid in form and it has Verticalscrol and HorizentalScroll.
how can i get postion of Scroll's DBGrid??

Handoko

  • Hero Member
  • *****
  • Posts: 1326
  • My goal: build my own game engine using Lazarus
Re: How can get scroll position of DBGrid ?
« Reply #1 on: June 12, 2017, 01:55:39 pm »
As far as I know, we cannot access the positions of DBGrid's scrollbars. Why do you need the values? Can you please explain more.

Alternatively, both TStringGrid and TDrawGrid have TopRow to get the first row that showing on the grid.

It may not the best thing to do but if you 'really' need to get the vertical scrollbar position, you can try to replace TDBGrid with TStringGrid. The downside is, you have to provide the code manually to load data from database to TStringGrid and that is not easy.

majid.ebru

  • Full Member
  • ***
  • Posts: 200
Re: How can get scroll position of DBGrid ?
« Reply #2 on: June 12, 2017, 02:18:27 pm »
Thank you

i put a scrollbox on top of DBGrid and put it some editbox.
i want to  search text in each colume.
but if i move verticalscroll of dbgrid , i can't move verticalscrollbox becuase i haven't verticalscroll position of DBgrid.
.
so i should an other way to search each colum. :-X

Handoko

  • Hero Member
  • *****
  • Posts: 1326
  • My goal: build my own game engine using Lazarus
Re: How can get scroll position of DBGrid ?
« Reply #3 on: June 12, 2017, 02:30:07 pm »
Did you mean you put a TScrollBox on the TDBGrid? You cannot use TScrollBox to scroll the things inside TDBGrid.

What did  you want to do? Do you want to do item searching for the database? User type something in a TEdit and the result will be shown in a grid?

majid.ebru

  • Full Member
  • ***
  • Posts: 200
Re: How can get scroll position of DBGrid ?
« Reply #4 on: June 12, 2017, 02:36:28 pm »
Did you mean you put a TScrollBox on the TDBGrid? You cannot use TScrollBox to scroll the things inside TDBGrid.
No.i can't put a TScrollBox on the TDBGrid
Do you want to do item searching for the database? User type something in a TEdit and the result will be shown in a grid?
YES

mangakissa

  • Hero Member
  • *****
  • Posts: 689
Re: How can get scroll position of DBGrid ?
« Reply #5 on: June 12, 2017, 02:56:01 pm »
Build a searchoption on top of the first cells of tdbgrid. Or use a tpanel, a tcombobox, a teditbox and a button for searching in the dataset (note: TDBrid only visualize your data). 
Lazarus 1.64 (32b) / FPC 3.0
Windows 10

majid.ebru

  • Full Member
  • ***
  • Posts: 200
Re: How can get scroll position of DBGrid ?
« Reply #6 on: June 12, 2017, 03:04:16 pm »
Build a searchoption on top of the first cells of tdbgrid. Or use a tpanel, a tcombobox, a teditbox and a button for searching in the dataset (note: TDBrid only visualize your data).

yes
i want to this.
.
but my fields are 65 fields and i can't show all of them . so i put a TScrollBox and put  this some panels and edit.

howardpc

  • Hero Member
  • *****
  • Posts: 2190
Re: How can get scroll position of DBGrid ?
« Reply #7 on: June 12, 2017, 04:34:02 pm »
Attached is a small example showing how locating a specific record in a dataset via a TEdit entry can scroll a dbgrid display.

Handoko

  • Hero Member
  • *****
  • Posts: 1326
  • My goal: build my own game engine using Lazarus
Re: How can get scroll position of DBGrid ?
« Reply #8 on: June 12, 2017, 05:29:31 pm »
I tried something different, show all results in a TListBox.

Note: I'm not good in database programming, I use only the easiest TDbf.  :D

majid.ebru

  • Full Member
  • ***
  • Posts: 200
Re: How can get scroll position of DBGrid ?
« Reply #9 on: June 12, 2017, 09:18:43 pm »
thank you

i want like this :

Handoko

  • Hero Member
  • *****
  • Posts: 1326
  • My goal: build my own game engine using Lazarus
Re: How can get scroll position of DBGrid ?
« Reply #10 on: June 12, 2017, 10:02:11 pm »
If I want to do the thing like the picture you showed, I will do like this:

01. Load all data into a StringGrid
02. Ask user to provide the filter string and which column to search
03. Compare each cell in the column with the filter string
04. If found, save the results of the row index into an array of integer
05. Now you have the column position and the array of integer
06. Highlight the cell using TStringGrid.OnPrepareCanvas

It's not hard, right? But you need to know how to load and save data to StringGrid, compare cell with data, save the result in an array and do cell highlight.

mangakissa

  • Hero Member
  • *****
  • Posts: 689
Re: How can get scroll position of DBGrid ?
« Reply #11 on: June 13, 2017, 08:47:06 am »
If you want to show a filter on the first row, you have to develop it self. But there are alternatives like
kgrid
Lazarus 1.64 (32b) / FPC 3.0
Windows 10

Handoko

  • Hero Member
  • *****
  • Posts: 1326
  • My goal: build my own game engine using Lazarus
Re: How can get scroll position of DBGrid ?
« Reply #12 on: June 13, 2017, 09:47:36 am »
so i should an other way to search each colum. :-X

What you said can have 2 meanings:
- Search items inside a column
- Locate a column by its name

The icon in the picture you shown previously is called filtering. Filtering is not the same as searching. Filtering means to hide cells/columns/rows that do not pass the test condition.

If what you want is searching, I've made an example for you. It used a TStringGrid. TStringGrid is much powerful than DBGrid, but you need to provide your code. If it is not enough, you may try KGrid as recommended by @mangakissa.

My example does not show you how to do filtering. But if you already understand how the code work, it won't be hard to modify it to support filtering.

Code: Pascal  [Select]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Graphics, dbf, db, Forms, StdCtrls, ExtCtrls, ComCtrls,
  9.   Grids;
  10.  
  11. type
  12.  
  13.   { TForm1 }
  14.  
  15.   TForm1 = class(TForm)
  16.     btnOpen: TButton;
  17.     btnSearch: TButton;
  18.     DataSource1: TDataSource;
  19.     Dbf1: TDbf;
  20.     Label1: TLabel;
  21.     LabeledEdit1: TLabeledEdit;
  22.     RadioGroup1: TRadioGroup;
  23.     StatusBar1: TStatusBar;
  24.     StringGrid1: TStringGrid;
  25.     procedure btnOpenClick(Sender: TObject);
  26.     procedure btnSearchClick(Sender: TObject);
  27.     procedure StringGrid1Click(Sender: TObject);
  28.     procedure StringGrid1PrepareCanvas(sender: TObject; aCol, aRow: Integer;
  29.       aState: TGridDrawState);
  30.   end;
  31.  
  32. const
  33.   DataFileName = 'MyData.dbf';
  34.  
  35. type
  36.   TPosition = record
  37.     Col: Integer;
  38.     Row: Integer
  39.   end;
  40.  
  41. var
  42.   Form1: TForm1;
  43.   Results: array of TPosition;
  44.  
  45. implementation
  46.  
  47. {$R *.lfm}
  48.  
  49. { TForm1 }
  50.  
  51. procedure TForm1.btnOpenClick(Sender: TObject);
  52. var
  53.   i: Integer;
  54. begin
  55.  
  56.   // Open the database
  57.   Dbf1.FilePathFull := ExtractFileDir(ParamStr(0));
  58.   Dbf1.TableName := DataFileName;
  59.   Dbf1.Open;
  60.  
  61.   // Set StringGrid's row and column
  62.   StringGrid1.RowCount := Dbf1.RecordCount + 1;
  63.   for i := 1 to Dbf1.FieldCount do
  64.   begin
  65.     StringGrid1.Columns.Add;
  66.     StringGrid1.Columns[i-1].Title.Caption := Dbf1.FieldDefs[i-1].Name;
  67.     StringGrid1.Columns[i-1].Title.Alignment := taCenter;
  68.     StringGrid1.Columns[i-1].Title.Font.Style := [fsBold];
  69.     StringGrid1.Columns[i-1].Width := 120;
  70.   end;
  71.  
  72.   // Load data to StringGrid
  73.   Dbf1.First;
  74.   while not(Dbf1.EOF) do
  75.   begin
  76.     StringGrid1.Cells[0, Dbf1.RecNo] := IntToStr(Dbf1.RecNo);
  77.     for i := 0 to (Dbf1.FieldCount-1) do
  78.       StringGrid1.Cells[i+1, Dbf1.RecNo] := Dbf1.Fields[i].AsString;
  79.     Dbf1.Next;
  80.   end;
  81.  
  82.   // Close the database
  83.   Dbf1.Close;
  84.  
  85.   StringGrid1.Enabled := True;
  86.   btnOpen.Enabled := False;
  87.   Label1.Visible := True;
  88.   Height := 380;
  89.   StringGrid1Click(Sender); // Show row:column info and paint header color
  90.  
  91. end;
  92.  
  93. procedure TForm1.btnSearchClick(Sender: TObject);
  94. var
  95.   c, r: Integer;
  96. begin
  97.  
  98.   // Abort search if user does not provide text to search
  99.   if (LabeledEdit1.Text = '') then
  100.   begin
  101.     StatusBar1.SimpleText := ' Error, please provide search text.';
  102.     Exit;
  103.   end;
  104.  
  105.   // Search items in a column
  106.   if (RadioGroup1.ItemIndex = 0) then
  107.   begin
  108.     SetLength(Results, 0);
  109.     for r := 0 to (StringGrid1.RowCount-1) do
  110.     begin
  111.       if Pos(LabeledEdit1.Text, StringGrid1.Cells[StringGrid1.Col, r]) > 0 then
  112.       begin
  113.         SetLength(Results, Length(Results)+1);
  114.         Results[High(Results)].Col := StringGrid1.Col;
  115.         Results[High(Results)].Row := r;
  116.       end;
  117.     end;
  118.     StringGrid1.Repaint;
  119.     StatusBar1.SimpleText :=
  120.       ' Search result: ' + IntToStr(Length(Results)) + ' item(s).';;
  121.   end;
  122.  
  123.   // Search all
  124.   if (RadioGroup1.ItemIndex = 1) then
  125.   begin
  126.     SetLength(Results, 0);
  127.     for c := 0 to (StringGrid1.Columns.Count-1) do
  128.       for r := 0 to (StringGrid1.RowCount-1) do
  129.       begin
  130.         if Pos(LabeledEdit1.Text, StringGrid1.Cells[c, r]) > 0 then
  131.         begin
  132.           SetLength(Results, Length(Results)+1);
  133.           Results[High(Results)].Col := c;
  134.           Results[High(Results)].Row := r;
  135.         end;
  136.       end;
  137.     StringGrid1.Repaint;
  138.     StatusBar1.SimpleText :=
  139.       ' Search result: ' + IntToStr(Length(Results)) + ' item(s).';;
  140.   end;
  141.  
  142. end;
  143.  
  144. procedure TForm1.StringGrid1Click(Sender: TObject);
  145. var
  146.   i: Integer;
  147. begin
  148.  
  149.   // Show StringGrid's row:column position
  150.   Label1.Caption := IntToStr(StringGrid1.Row) +':'+ IntToStr(StringGrid1.Col);
  151.  
  152.   // Change StringGrid header color
  153.   for i := 0 to (StringGrid1.Columns.Count-1) do
  154.     if (i = StringGrid1.Col-1) then
  155.       StringGrid1.Columns[i].Title.Font.Color := clRed
  156.     else
  157.       StringGrid1.Columns[i].Title.Font.Color := clBlack;
  158.  
  159.   // Change info in search method option
  160.   RadioGroup1.Items[0] := StringGrid1.Columns[StringGrid1.Col-1].Title.Caption;
  161.  
  162. end;
  163.  
  164. procedure TForm1.StringGrid1PrepareCanvas(sender: TObject; aCol, aRow: Integer;
  165.   aState: TGridDrawState);
  166. var
  167.   i: Integer;
  168. begin
  169.  
  170.   if not(sender is TStringGrid) then Exit;
  171.  
  172.   // Paint the background yellow for the results
  173.   for i := Low(Results) to High(Results) do
  174.     if (Results[i].Col = aCol) and (Results[i].Row = aRow) then
  175.       (sender as TStringGrid).Canvas.Brush.Color := clYellow;
  176.  
  177. end;
  178.  
  179. end.
« Last Edit: June 13, 2017, 09:51:25 am by Handoko »

Thaddy

  • Hero Member
  • *****
  • Posts: 3987
Re: How can get scroll position of DBGrid ?
« Reply #13 on: June 13, 2017, 10:41:01 am »
In general, you would not want to use scroll positions over a whole database. You should want to get rid of the scrollbars altogether. In database programming, scrollbars are evil. >:( Except maybe on a small selection of 10.000 records.
"Logically, no number of positive outcomes at the level of experimental testing can confirm a scientific theory, but a single counterexample is logically decisive."

majid.ebru

  • Full Member
  • ***
  • Posts: 200
Re: How can get scroll position of DBGrid ?
« Reply #14 on: June 13, 2017, 07:48:47 pm »
Thanks
.
i don't like use component( because i want wirte code myself).
.
i thinck there isn't deferent between search and filter.
.
i just want that Tedit exactly stay on culome and when user move scroll of DBGrid,i can move it.
.
ok.i try to use TStringGrid.
.
thanks every body

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus