자유롭게 질의 및 응답을 할 수 있는 게시판입니다. 개발자 여러분의 답변이 큰 도움이 됩니다. 
  • 제품설치/등록 오류 문의: 설치/등록 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 39641
공지 유용한 관련 사이트 관리자2 2014.03.20 79142
1032 1msec 이하 타임클럭 문의!! [4] 산으로가자요 2018.01.18 907
1031 xe7로 DataSnap 사용하여 3티어로 작성 한 프로그램 remort error: Error reading servermethods2.clientheight: property clientheight does not exist오류가 납니다 [3] file delphi 2016.03.29 907
1030 엑셀 저장 오류 [5] 똘귀아빠 2020.04.27 908
1029 C++ Builder 10.3 / 10.3.1 Clang에서 Code Insight 동작하지 않는 현상 질의? [3] 포비 2019.02.21 909
1028 Delphi 10.3 Rio 를 통한 최초 Android 개발시 오류 [5] file 이치고 2019.04.24 909
1027 [XE7] 샘플 프로그램 속도 문제 [1] 마로 2015.02.26 910
1026 Kinvey 의 Custom Endpoint 문서 내용이 이상합니다. [1] 타락천사 2014.12.18 910
1025 [공유] 파이어몽키 콤보박스의 글자 크기를 변경하고 싶습니다. 험프리 2019.02.20 911
1024 centos 6.9 환경에 PA서버 설치시 문의입니다. [3] 운땡최향태 2017.05.10 912
1023 XE8 WebBrowser컴포넌트 메모리 해제법 가을이다 2015.10.16 912
1022 Datasnap client 어플리케이션을 FireDAC으로 만들수 있나요? [1] 금성우 2015.01.21 912
1021 TBitmap.Savetofile 관련 질문 [1] hsson 2016.04.05 913
1020 SMS 핸들러 권한 추가 문의드려요. [2] 생각 2019.01.23 914
1019 [XE6] 컴포넌트 기본폰트 설정법 가을이다 2015.02.26 917
1018 앱설치시 홈화면에 바로가기 만들기 문의 [2] 김태윤 2016.04.07 917
1017 Rest 파라미터 한글 문제...? 헨씀히포 2017.10.11 918
1016 델파이로 Jsp,php 등 웹서버와 통신할수 있나요? [2] 드레곤 2016.05.26 923
1015 비콘 감지중 블루투스를 사용자가 강제로 off시 어떻게 처리해야 할까요? 김태윤 2016.07.15 924
1014 인쇄 대화상자가 표시되지 않고 인쇄하는 방법 험프리 2019.02.12 924
1013 XE8 Android에서 ini 파일 관련 문의 드립니다. [1] 비도리 2015.12.21 925