Discussion:
Question on TDBGrid.
(too old to reply)
Cooper
2010-03-21 17:29:26 UTC
Permalink
Hello,
i have a array to two dimension for example 5 column and 50 row. I want
import this array and relative values row/column in a tdbgrid.
I have solved about name of column; but not understood as i can to adding it
tdbgrid row from my array. I have tried to search in google but not found
much that can help me, just found something about sort, color etc that at
moment not interest me.
Thank all that can help me.
Cooper.
BRoberts
2010-03-22 01:44:19 UTC
Permalink
Post by Cooper
Hello,
i have a array to two dimension for example 5 column and 50 row. I want
import this array and relative values row/column in a tdbgrid.
I have solved about name of column; but not understood as i can to adding
it tdbgrid row from my array. I have tried to search in google but not
found much that can help me, just found something about sort, color etc
that at moment not interest me.
dbGrids are intended for use with database tables. If you have an array you
might want to consider a string grid.
Cooper
2010-03-22 09:35:46 UTC
Permalink
Post by BRoberts
dbGrids are intended for use with database tables. If you have an array
you might want to consider a string grid.
Yes, i have thinked about string grid, but problem is that in this grid i
need sort too for column, thing that with string grid i can't to do; just
only with tdgrid.
At me need necessary use a tdgrid. Can i load value from array to temp
database in memory and from there load in tdgrid?
If i can to to it, just a example for to it? Me not found nothing about it
with google.
Suppose that my database is a array, where value are setted from a previous
elaboration.
Thanks again very much.
Cooper
2010-03-22 12:51:47 UTC
Permalink
Post by Cooper
Post by BRoberts
dbGrids are intended for use with database tables. If you have an array
you might want to consider a string grid.
Yes, i have thinked about string grid, but problem is that in this grid i
need sort too for column, thing that with string grid i can't to do; just
only with tdgrid.
At me need necessary use a tdgrid. Can i load value from array to temp
database in memory and from there load in tdgrid?
If i can to to it, just a example for to it? Me not found nothing about it
with google.
Suppose that my database is a array, where value are setted from a
previous elaboration.
Thanks again very much.
Hello, maybe me understood as solve problem with dbgrid and array. I should
to use a datasource. In mine case datasource is a array to two dimension.
Searching on web me found this:

- http://www.xtremedotnettalk.com/showthread.php?t=89458

But is in c, and me not understand very much from there.
Someone can give me more help? Just a link, or some example for can put in a
dbgrid a multimensional array?
Thanks you again very much.
Cooper.
a***@aol.com
2010-03-22 21:16:22 UTC
Permalink
Post by Cooper
Yes, i have thinked about string grid, but problem is that in this grid i
need sort too for column, thing that with string grid i can't to do; just
only with tdgrid.
TStringGrid has a property Rows, which is a TStrings with the column
contents as individual elements in the TStrings (accessed as a
TStringList).

To sort you write a loop for all rows which checks whether any element
of one row is greater or less than the same element in the next row.
If it is greater then you swap the two rows (there is a TStringGrid
function to swap rows). You keep looping until the grid is sorted
(that is, the appropriate element in each row is less than the element
in the next row).

To do the actual check you write a compare function which takes
references to two rows and does the comparison on the stated elements
of the two rows, returning 0 if they're the same, negative if the
first is less than the second, and positive if the first is greater
than the second. What is "less" or "greater" is up to you when you
write the compare function.

Here is one I use ...

type
TMoveStrGrid = class(TCustomGrid);
(necessary to access the protected MoveRow procedure)

TGridSortCompare = function(Row1, Row2 : TStrings) : integer;
{returns -1 if Row1 < Row2, 0 if Row1 = Row2, & +1 if Row1 > Row2}

implementation

function GridSortCompare(RowA, RowB : TStrings ) : integer;
{must return 0 if equal, negative if RowA < RowB, positive if RowA >
RowB
const
TextCol = 4; // index of column to sort on
begin
Result := AnsiCompareText(RowA[TextCol], RowB[TextCol]);
end;

procedure SortStringGrid(StringGrid : TStringGrid;
CompareProc : TGridSortCompare);
{*D 14/11/2000}
{sort a stringgrid}
var
Sorted : boolean;
SelRow, TopVisibleRow, i : integer;
begin
with StringGrid do begin
TopVisibleRow := TopRow;
SelRow := Row;
repeat {until Sorted}
Sorted := true;
for i := 2 to RowCount - 1 do begin // row 0 is a fixed row
if CompareProc(Rows[i-1], Rows[i]) > 0 then begin
{... then Row[i-1] is > Row[i] but should be <, therefore
swap}
Sorted := false; // set flag for continuing to sort
{swap stringgtid rows, MoveRow is protected so typecast to
descendant}
TMoveStrGrid(StringGrid).MoveRow(i, i-1);
end {if CompareProc(Rows[i-1], Rows[i]) > 0}
end; {for i := 2 to RowCount - 1}
until Sorted;
{restore top row & selected row}
TopRow := TopVisibleRow;
Row := SelRow;
end; {with StringGrid}
end;

Alan Lloyd
Cooper
2010-03-22 21:42:24 UTC
Permalink
Post by a***@aol.com
TStringGrid has a property Rows, which is a TStrings with the column
contents as individual elements in the TStrings (accessed as a
TStringList).
[cut]
Thanks for your answer; i try; about sort, i need to give possibility to
user clicking to column to sort table for value of that column.
Looking about tstringgrid, i saw that about column not is clickable :( in
this sense i tell about sort :(
Sorry if not was right in last post :(
Cooper.
a***@aol.com
2010-03-23 10:24:02 UTC
Permalink
Post by Cooper
Thanks for your answer; i try; about sort, i need to give possibility to
user clicking to column to sort table for value of that column.
Looking about tstringgrid, i saw that about column not is clickable :( in
this sense i tell about sort :(
Use TStringGrid.OnMouseDown to get the mouse position in the
stringgrid, then use TStringGrid.MouseToCell to get the column & row.
If the row is zero then sort the stringgrid appropriately to the
column.

procedure TMyForm.StringGridMouseDown(Sender: TObject; Button:
TMouseButton;
Shift: TShiftState; X, Y:
Integer);
var
ARow, ACol : integer;
begin
with StringGrid do begin
MouseToCell(X, Y, ACol, ARow);
if (ARow = 0) then begin
case ACol of
0 : {etc etc add coding to call the sort procedure}

If you write a sort procedure with an additional parameter to take the
column, then that parameter can be used to call the appropriate
CompareSort function. Or add another parameter of column id to the
CompareSort function and use that to code appropriately in that
function.

If you declare an enumerated type with meaningful value names. then
that will enable clarity in your code. I used ...

type
TGridSortType = (stNone, stText1, stText2, stText3, stDateTime1,
stDateTime2,
stDateTime3, stPriority, stVIPPriority, stDictLen);

The value names are the contents of the columns in a TStringGrid I
used to display data. Hence calling for a sort with a parameter of
stPriority (== column 7) makes my code clear it is sorting on
Priority. Just typecast the ACol above to a TGridSortType and pass it
as that.

Note that enumerated types have values from 0 upwards.

Alan Lloyd

Loading...