Итак, строковая сетка, которую я имею в программе, которую я упомянул здесь: (Delphi - Изменение цвета вкладки активной страницы и его сброс после нажатия на другую вкладку) отлично сортирует от наименьшего к наибольшему, но сетка в некотором роде дает сбои / выходит из строя. Отсортированные строки выбрасываются в конец сетки (иногда даже без номеров строк или строки сохраняют свои номера). Итак, моя проблема заключается в том, как я могу отсортировать свою сетку строк, не разрушая сетку полностью в процессе?
Код, который я использую:
//code that sorts the grid
procedure TfrmPuntehou.SortSTLGrid(var grid:TStringGrid; columntotal:integer);
const
separator = ',';
var
iCount,i,j,k,iPos:integer;
TheList:TStringList;
sString,sTempString:string;
begin
//procedure to sort from small to large values
//get row amount
iCount:=grid.RowCount;
//create list
TheList:=TStringList.Create;
TheList.Sorted:=False;
//start of try..finally block
try
begin
//fill the list
for i := 1 to (iCount - 1) do
begin
TheList.Add(grid.Rows[i].Strings[columntotal]+separator+grid.Rows[i].Text);
end;
//sort the list
TheList.Sort;
for k := 1 to TheList.Count do
begin
//take the line of the list and put it in a string var
sString:= TheList.Strings[(k-1)];
//get separator pos in that string
iPos:=AnsiPos(separator,sString);
sTempString:='';
//remove separator and the column text at the front of the string
sTempString:=Copy(sString,(iPos+2),Length(sString));
TheList.Strings[(k-1)]:= '';
TheList.Strings[(k-1)]:= sTempString;
end;
//fill the grid
for j:= 1 to (iCount - 1) do
begin
grid.Rows[j].Text := TheList.Strings[(J-1)] ;
end;
end;
finally
TheList.Free;
end;
//end of try..finally block
end;
//code that I use to customize the grid on startup
procedure TfrmPuntehou.TitlesAndNumbering(grid: TStringGrid);
var
i:integer;
begin
//procedure that customizes the grid entered as a parameter
with grid do
begin
//names the columns
Cells[0,0]:='Row Number';
Cells[1,0]:='Car Number';
Cells[2,0]:='Name';
Cells[3,0]:='Licence';
Cells[4,0]:='Heat 1';
Cells[5,0]:='Heat 2';
Cells[6,0]:='Subtotal 1';
Cells[7,0]:='Heat 3';
Cells[8,0]:='Subtotal 2';
Cells[9,0]:='Final';
Cells[10,0]:='Final Total';
//for loop to number the rows
for i := 1 to rowMax do
begin
Cells[0,i]:=IntToStr(i);
end;
//end of for
//other extra settings
RowCount:=rowMax;
ColCount:=colMax;
DefaultColWidth:=100;
FixedCols:=0;
FixedRows:=1;
end;
end;
Скриншоты, иллюстрирующие проблему:
Заранее спасибо за помощь!
С уважением
PrimeBeat