grid sorting ...
#1
Posted 14 June 2007 - 12:40 PM
i want to sort my grid by rows and in the way the columns are ordered.
In detail: if i compare row 1 and row 2 then at first i compare the first column (order by position, not by index!) from row 1 and row 2. If both values are equal i compare the second column and so on ... till i have a difference (or no more columns are left).
I tried this using custom sort and changing the sorted column after a column move in the column move event, but in the compare procedure i get Cell1 and Cell2 and don't know how to get the other Cells if this one are equal. I would need the row information from Cell1 and Cell2 to check the other Cells, but Cell1.RowIndex and Cell2.RowIndex are always identical, so something is wrong - either the Rowindex or my concept.
Or is there another way?
thanks,
user22
#2
Posted 14 June 2007 - 01:40 PM
I recommend reading next article first:
http://www.bergsoft....page=customsort
there is also demo attached and you may modify it for your needs.
Cell.RowIndex is empty when accessed to it directly, something else need to be done
Best regards
--
BergSoft Home Page: www.bergsoft.net
Users Section: users.bergsoft.net
Articles and Tutorials: help.bergsoft.net (Developers Network)
--
BergSoft Facebook page
--
Send us applications made with our components and we will submit them on: www.bergsoft.net/apps.htm. Link to this page will be also set on home page too.
#3
Posted 14 June 2007 - 02:30 PM
--
BergSoft Home Page: www.bergsoft.net
Users Section: users.bergsoft.net
Articles and Tutorials: help.bergsoft.net (Developers Network)
--
BergSoft Facebook page
--
Send us applications made with our components and we will submit them on: www.bergsoft.net/apps.htm. Link to this page will be also set on home page too.
#4
Posted 14 June 2007 - 02:59 PM
i have read this article very carefully before, believe me :-) But i need the possibility that if Cell1 and Cell2 are equal i can compare two values from another column in the row of Cell1 respectively Cell2.
bye,
user22
Or maybe we can have something like a SortRow function where i get two rownumbers instead of two Cells??
#5
Posted 14 June 2007 - 03:26 PM
Maybe function GetCellIndex may help for this situation? I will need to add it first.
Best regards
--
BergSoft Home Page: www.bergsoft.net
Users Section: users.bergsoft.net
Articles and Tutorials: help.bergsoft.net (Developers Network)
--
BergSoft Facebook page
--
Send us applications made with our components and we will submit them on: www.bergsoft.net/apps.htm. Link to this page will be also set on home page too.
#6
Posted 14 June 2007 - 04:07 PM
if GetCellIndex will help me to find the row from this Cell, then yes.
But what do you think about my suggestion of a row-depending sort?
thank you very much,
user22
#7
Posted 14 June 2007 - 04:10 PM
I will add it now. I will send you code when I finish it.
Can you please give me more details about this event. As I understand, it is similar to OnCompare but with RowIndex parameter?
regards
--
BergSoft Home Page: www.bergsoft.net
Users Section: users.bergsoft.net
Articles and Tutorials: help.bergsoft.net (Developers Network)
--
BergSoft Facebook page
--
Send us applications made with our components and we will submit them on: www.bergsoft.net/apps.htm. Link to this page will be also set on home page too.
#8
Posted 14 June 2007 - 05:26 PM
I have add 2 methods:
In NxCells.pas, in public of TCells add:
var
i: Integer;
begin
for i := 0 to Pred(FColCount) do
begin
Result := FCellsList[i].IndexOf(ACell);
if Result <> -1 then Exit;
end;
end;
In NxGrid.pas, in public of TNextGrid add:
begin
Result := FCells.GetCellRowIndex(ACell);
end;
I hope that this is it
regards
--
BergSoft Home Page: www.bergsoft.net
Users Section: users.bergsoft.net
Articles and Tutorials: help.bergsoft.net (Developers Network)
--
BergSoft Facebook page
--
Send us applications made with our components and we will submit them on: www.bergsoft.net/apps.htm. Link to this page will be also set on home page too.
#9
Posted 14 June 2007 - 06:30 PM
i tried to add it but i get the compiler error that ACol in new function TCells.GetCellRowIndex(ACell: TCell) in NxCells.pas is not defined ??
thanks,
Helmut
... and yes, SortRows method should work like normal sort but instead of using OnCompare with two Cells we have two rownumbers as parameter and depending on the return value you do nothing or you exchange the two rows (like in onCompare).
Maybe we will name the event OnCompareRows(rowindex1, rowindex2): integer and i can start it with ... myNextGrid.SortRows;
But it would be good if SortRows can work without OnCompareRows() too - if this event is not defined, it sorts internally using the cells depending on the datatype and it uses the visible order of the rows. So it compares the two cells in the first column, if they are equal then it compares the cells in the second column ...
This is the functionality i need as fast as possible, so i try to do all the necessary steps in my application and so i need the row info.
But then at next we could think about using hidden columns too or not or only using hidden columns, having a property "sortAfterColMoving" where grid executes SortRows automatically after a column has been moved to another position, ... and so on
#10
Posted 14 June 2007 - 06:35 PM
I have Edit post before you post for code in TCells. Please try again
If you like, I may add OnCompareRows now. Both events will be called, OnCompare for cell, OnCompareRow for Row. If this is ok, I will add it now.
regards
--
BergSoft Home Page: www.bergsoft.net
Users Section: users.bergsoft.net
Articles and Tutorials: help.bergsoft.net (Developers Network)
--
BergSoft Facebook page
--
Send us applications made with our components and we will submit them on: www.bergsoft.net/apps.htm. Link to this page will be also set on home page too.
#11
Posted 14 June 2007 - 07:45 PM
i have solved the compiler-error in changing ACol to ACell.ColumnIndex
Hope this is okay, the first sorting tests where fine.
And here is the source what i do in the moment to have "RowSorting", maybe you have some comments about it (or you see something is wrong):
procedure TFoList.CompareRows(Sender: TObject; Cell1, Cell2: TCell;
var Compare: Integer);
var x, col, r1, r2: integer;
ng: TNextGrid;
begin
ng := TNextGrid(sender);
r1 := ng.GetCellRowIndex(Cell1);
r2 := ng.GetCellRowIndex(Cell2);
compare := 0;
for x := 0 to ng.Columns.Count - 1 do begin
col := ng.Columns.PositionItem[x].Index;
if ng.Cells[col,r1] = ng.Cells[col,r2] then continue;
case ng.Columns[col].ColumnType of
ctBoolean:
if ng.Cell[col,r1].AsBoolean // true/false is different, but what is higher?
then compare := 1 else compare := -1;
ctAutoInc, ctInteger:
if ng.Cell[col,r1].AsInteger > ng.Cell[col,r2].AsInteger
then compare := 1 else compare := -1;
ctDate, ctFloat:
if ng.Cell[col,r1].AsFloat > ng.Cell[col,r2].AsFloat
then compare := 1 else compare := -1;
ctGuid, ctString:
if ng.Cells[col,r1] > ng.Cells[col,r2]
then compare := 1 else compare := -1;
end;
if compare <> 0 then break; // don't compare columns like ctGraphic, ctVirtual, ...
end;
end;
As you can see, with GetCellRowIndex i get the same information as i would get with OnCompareRows, so there is not really a need for it now, i think.
thank you very much!
#12
Posted 14 June 2007 - 08:12 PM
I will do some testings now, and then answer you.
regards
--
BergSoft Home Page: www.bergsoft.net
Users Section: users.bergsoft.net
Articles and Tutorials: help.bergsoft.net (Developers Network)
--
BergSoft Facebook page
--
Send us applications made with our components and we will submit them on: www.bergsoft.net/apps.htm. Link to this page will be also set on home page too.
#13
Posted 14 June 2007 - 11:42 PM
I have do some research. It will a releative easy to add OnRowCompare event. If you still need it urgently, I may add it.
In next period, I will automatically update ColIndex and RowIndex for cell, so RowIndex will be able to read from Cell directly.
regards
--
BergSoft Home Page: www.bergsoft.net
Users Section: users.bergsoft.net
Articles and Tutorials: help.bergsoft.net (Developers Network)
--
BergSoft Facebook page
--
Send us applications made with our components and we will submit them on: www.bergsoft.net/apps.htm. Link to this page will be also set on home page too.
#14
Posted 15 June 2007 - 09:31 AM
in the moment i have a solution i can live with, so there is no need to implement something urgently. I would prefer to have ColIndex and RowIndex with actual values in Cell object when OnCompare is executed.
best regards
#15
Posted 22 September 2011 - 12:32 PM
Could you please explain how you managed to get it working?
Your CompareRows procedure looks fine, but when do you call it?
Thanks in advance.
#16
Posted 10 October 2011 - 05:44 PM
my grids where always sorted by column sequence - so for example if a grid is sorted by name and the user wants to sort it by birthdate then he moves the birthdate column in first position and the grid knows now that it has to sort the data by date (and persons with same date are sorted by name, which is now the second column, and so on).
bye,
Helmut
0 user(s) are reading this topic
0 members, 0 guests, 0 anonymous users











