자유롭게 질의 및 응답을 할 수 있는 게시판입니다. 개발자 여러분의 답변이 큰 도움이 됩니다. 
  • 제품설치/등록 오류 문의: 설치/등록 Q&A 이용 (제품 구매 고객 한정)

Q, 

스트링그리드를 정렬하고 싶습니다.


A.

다음 소스코드를 참고하시기 바랍니다.

GridSortSource.zip 

스트링그리드 컬럼을 선택하면 해당 컬럼으로 정렬됩니다.


{ ************** SortGrid ************* }

procedure TForm1.SortGrid(Grid: TStringGrid; SortCol: Integer);

{ A simple exchange sort of grid rows }

var

  I, J: Integer;

  temp: TStringList;

begin

  temp := TStringList.create;

  with Grid do

    for I := FixedRows to RowCount - 2 do { because last row has no next row }

      for J := I + 1 to RowCount - 1 do { from next row to end }

        if AnsiCompareText(Cells[SortCol, I], Cells[SortCol, J]) > 0 then

        begin

          temp.assign(Rows[J]);

          Rows[J].assign(Rows[I]);

          Rows[I].assign(temp);

        end;

  temp.free;

end;


procedure TForm1.FormCreate(Sender: TObject);

var

  I, J: Integer;

  totwidth: Integer;

begin

  with StringGrid1 do

  begin

    { assign some random numbers }

    for J := 1 to RowCount - 1 do

      for I := 1 to colcount - 1 do

        Cells[I, J] := Format('%3d', [Random(1000)]);


    { Add row labels }

    for I := 1 to RowCount - 1 do

      Cells[0, I] := 'Row ' + Format('%2d', [I]);


    { Add column labels and change column widths }

    totwidth := 0;

    for I := 0 to colcount - 1 do

    begin

      Cells[I, 0] := 'Column ' + IntToStr(I + 1);

      { vary column widths for testing }

      Colwidths[I] := DefaultColWidth + Random(48);

      Inc(totwidth, Colwidths[I]);

    end;

    { adjust grid width so that all column are visible }

    Width := totwidth + (colcount + 3) * GridLineWidth;

  end;

end;


{ ************ StringgridMouseUp ************** }

procedure TForm1.StringGrid1MouseUp(Sender: TObject; Button: TMouseButton;

  Shift: TShiftState; X, Y: Integer);

var

  c, J: Integer;

  rect: TRect;

begin

  with StringGrid1 do

    if Y < RowHeights[0] then { make sure row 0 was clicked }

    begin

      for J := 0 to colcount - 1 do { determine which column was clicked }

      begin

        rect := CellRect(J, 0);

        if (rect.Left < X) and (rect.Right > X) then

        begin

          c := J;

          break;

        end;

      end;

      SortGrid(StringGrid1, c);

    end;

end;

=============

string grid sort.txt 

string grid sort2.txt

번호 제목 글쓴이 날짜 조회 수
공지 [프로그래밍 강의] 2021.6~2021.12 관리자 2015.01.22 15802
공지 유용한 관련 사이트 관리자2 2014.03.20 54604
공지 본 게시판은 개발자 여러분들의 질문과 답변을 공유하는 공간입니다. 관리자 2012.01.10 98039
273 JSON 파라메타 한글 문제 [2] file 케이피 2021.08.26 1988
272 안드로이드 가상머신... [1] 파빌신 2014.05.05 1988
271 (미해결)XE6 커스텀 스타일 적용한 폼에서 팝업메뉴 버그 [3] file shocks 2014.06.17 1997
270 블루투스 프린터 출력을 하려고 합니다 [3] silkroad99 2016.11.16 2007
269 XE5 Trail Version 설치 시 C++ Builder 에서는 "FireMonkey Mobile Application"을 지원하지 않나요? [1] J7 2014.04.08 2017
268 비콘 거리 측정에 대해 문의 드립니다. [2] 쿠키 2017.04.14 2031
267 IdTcpServer와 IdTcpClient를 이용한 Sample 예제 있나요? [2] 거북선생 2016.10.26 2032
266 Delphi XE7 평가판을 이용하여 iOS 샘플 앱을 만들던 중 문의사항이 있어 메일을 드립니다. [5] radio 2015.03.14 2032
265 자동차 인식 방법에 대한 질문입니다. [4] file 거북선생 2021.08.25 2035
264 XE5 Android에 TWebbrowser를 사용할 때.. [2] ScorpioM 2014.04.15 2043
263 combobox 로 lookup 구현방법 [1] file 정헌이 2014.06.04 2059
262 [공유] 웹뷰를 이용한 하이브리드 앱 개발 시 궁금한 점 험프리 2017.08.02 2065
261 [문의]android target에 다른 pc emulator 추가 하는방법 [1] 강성범 2014.09.02 2072
» [공유] 스트링그리드(TStringGrid)를 정렬하고 싶어요. file 험프리 2015.10.27 2074
259 [질문] DataSnap에서 메시지 전달방법 [1] cdrd 2014.03.25 2079
258 [세미나] 휴대폰번호 인증서비스도 지원하나요? [1] Humphery 2014.07.30 2098
257 XE7에서 FireDAC를 이용하여 AccessDB사용시 배포 [3] 투덜이스머프 2014.12.10 2098
256 [마이그레이션][64bit] 64비트에서 CreateFileMapping의 반환값(핸들)이 0으로 표시됩니다. 험프리 2017.01.31 2103
255 델파이와 오라클 호환성 [1] 오라클매니아 2014.03.06 2125
254 Delphi XE7 kbmmemtable (CodeGear Edition) 설치 오류 [1] 도사 2014.10.06 2131