Главная страница
    Top.Mail.Ru    Яндекс.Метрика
Форум: "Основная";
Текущий архив: 2004.04.18;
Скачать: [xml.tar.bz2];

Вниз

Сортировка в TStringGrid   Найти похожие ветки 

 
mike_forewer ©   (2004-04-01 23:47) [0]

Help! Подскажите, как в TStringGrid сделать сортировку строк по возрастанию/убыванию данных в каком-нить столбце ?


 
[BAD]Angel ©   (2004-04-02 00:10) [1]

я, когда был маленький, засовывал содержимое в листбокс, проводил сортировку там, а потом пихал назад в стринггрид :)
...
а вообще есть компонента, чтобы не мучаться называется она AH_COMP или ALI_GRID это одно и то же :) если не найдешь обращайся, будет время скину ~ 400Кб


 
Ozone ©   (2004-04-02 06:24) [2]

А ручками слабо?


 
ssk   (2004-04-02 09:32) [3]

вот нарыл где-то когда-то (может даже на Торри), но в работе не пробовал


procedure SortStringGrid(var GenStrGrid: TStringGrid; ThatCol: Integer);
const
 // Define the Separator
 TheSeparator = "@";
var
 CountItem, I, J, K, ThePosition: integer;
 MyList: TStringList;
 MyString, TempString: string;
begin
 // Give the number of rows in the StringGrid
 CountItem := GenStrGrid.RowCount;
 //Create the List
 MyList        := TStringList.Create;
 MyList.Sorted := False;
 try
   begin
     for I := 1 to (CountItem - 1) do
       MyList.Add(GenStrGrid.Rows[I].Strings[ThatCol] + TheSeparator +
         GenStrGrid.Rows[I].Text);
     //Sort the List
     Mylist.Sort;

     for K := 1 to Mylist.Count do
     begin
       //Take the String of the line (K – 1)
       MyString := MyList.Strings[(K - 1)];
       //Find the position of the Separator in the String
       ThePosition := Pos(TheSeparator, MyString);
       TempString  := "";
       {Eliminate the Text of the column on which we have sorted the StringGrid}
       TempString := Copy(MyString, (ThePosition + 1), Length(MyString));
       MyList.Strings[(K - 1)] := "";
       MyList.Strings[(K - 1)] := TempString;
     end;

     // Refill the StringGrid
     for J := 1 to (CountItem - 1) do
       GenStrGrid.Rows[J].Text := MyList.Strings[(J - 1)];
   end;
 finally
   //Free the List
   MyList.Free;
 end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
 // Sort the StringGrid1 on the second Column
 // StringGrid1 nach der 1. Spalte sortieren
 SortStringGrid(StringGrid1, 1);
end;


 
ssk   (2004-04-02 09:33) [4]

и вот еще


type
 TMoveSG = class(TCustomGrid); // reveals protected MoveRow procedure

{...}

procedure SortGridByCols(Grid: TStringGrid; ColOrder: array of Integer);
var
 i, j:   Integer;
 Sorted: Boolean;

function Sort(Row1, Row2: Integer): Integer;
var
 C: Integer;
begin
 C      := 0;
 Result := AnsiCompareStr(Grid.Cols[ColOrder[C]][Row1], Grid.Cols[ColOrder[C]][Row2]);
 if Result = 0 then
 begin
   Inc(C);
   while (C <= High(ColOrder)) and (Result = 0) do
   begin
     Result := AnsiCompareStr(Grid.Cols[ColOrder[C]][Row1],
       Grid.Cols[ColOrder[C]][Row2]);
     Inc(C);
   end;
 end;
end;

begin
 if SizeOf(ColOrder) div SizeOf(i) <> Grid.ColCount then Exit;

 for i := 0 to High(ColOrder) do
   if (ColOrder[i] < 0) or (ColOrder[i] >= Grid.ColCount) then Exit;

 j := 0;
 Sorted := False;
 repeat
   Inc(j);
   with Grid do
     for i := 0 to RowCount - 2 do
       if Sort(i, i + 1) > 0 then
       begin
         TMoveSG(Grid).MoveRow(i + 1, i);
         Sorted := False;
       end;
 until Sorted or (j = 1000);
 Grid.Repaint;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
 { Sort rows based on the contents of two or more columns.
   Sorts first by column 1. If there are duplicate values
   in column 1, the next sort column is column 2 and so on...}
 SortGridByCols(StringGrid1, [1, 2, 0, 3, 4]);
end;



Страницы: 1 вся ветка

Форум: "Основная";
Текущий архив: 2004.04.18;
Скачать: [xml.tar.bz2];

Наверх





Память: 0.46 MB
Время: 0.03 c
14-1079980602
Adder
2004-03-22 21:36
2004.04.18
Мдя... комментарий кулпрограммера для тестера...


8-1062586676
raiks
2003-09-03 14:57
2004.04.18
Подскажите, как записать звук с микрофона в файл


8-1072167253
Василич
2003-12-23 11:14
2004.04.18
вывод синуса,звука на выход звуковой карты


3-1079589072
Nax
2004-03-18 08:51
2004.04.18
Два вопроса по синтаксису TQuery.SQL


1-1080680996
erusto
2004-03-31 01:09
2004.04.18
Отчеты через Rave





Afrikaans Albanian Arabic Armenian Azerbaijani Basque Belarusian Bulgarian Catalan Chinese (Simplified) Chinese (Traditional) Croatian Czech Danish Dutch English Estonian Filipino Finnish French
Galician Georgian German Greek Haitian Creole Hebrew Hindi Hungarian Icelandic Indonesian Irish Italian Japanese Korean Latvian Lithuanian Macedonian Malay Maltese Norwegian
Persian Polish Portuguese Romanian Russian Serbian Slovak Slovenian Spanish Swahili Swedish Thai Turkish Ukrainian Urdu Vietnamese Welsh Yiddish Bengali Bosnian
Cebuano Esperanto Gujarati Hausa Hmong Igbo Javanese Kannada Khmer Lao Latin Maori Marathi Mongolian Nepali Punjabi Somali Tamil Telugu Yoruba
Zulu
Английский Французский Немецкий Итальянский Португальский Русский Испанский