자유롭게 질의 및 응답을 할 수 있는 게시판입니다. 개발자 여러분의 답변이 큰 도움이 됩니다. 
  • 제품설치/등록 오류 문의: 설치/등록 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 17260
공지 유용한 관련 사이트 관리자2 2014.03.20 56157
공지 본 게시판은 개발자 여러분들의 질문과 답변을 공유하는 공간입니다. 관리자 2012.01.10 99579
1039 StringGrid와 Query Component와의 바인딩에 대한 질문입니다 투덜이스머프 2014.12.29 738
1038 windows 7 , delphi xe5 에서의 LoadLibrary 가 실행되지 않습니다. [1] 쭈니쿤 2016.01.12 741
1037 [질문] TMemo, TText에서 문자단위로 WordWrap(줄바꿈) 하는 방법 [2] file 푸른솔 2016.12.21 744
1036 [질문] 블루투스 송,수신... [2] file 천재일우 2016.12.28 745
1035 component 추가 하는 방법 알려주세요. [2] del 2018.01.24 745
1034 [XE8 출시세미나] EMS 푸쉬에서 클라이언트에서 푸쉬메시지 수신확인이 가능한가요? [1] Humphery 2015.04.30 745
1033 안드로이드 플랫폼에서 jpg, png파일 포맷 사용하려면 [2] superman 2016.06.15 748
1032 윈도우10 크리에이터스 업데이트 후 DateTimePicker 관련 오류문제 [3] 루키 2017.05.02 749
1031 앱이 먹통이되는 현상이 있어서 문의드립니다. [4] 최민철 2015.05.28 749
1030 Rest 파라미터 한글 문제...? 헨씀히포 2017.10.11 750
1029 델파이 6 소스 컴파일 및 빌드 오류 건 문의드립니다. [1] 다니엘 2017.09.14 751
1028 델파이 xe 디버깅시 값확인 [1] 가나다 2019.03.08 751
1027 mysql 연동관련하여 질문합니다. [1] sj9418 2019.03.20 752
1026 반투명 판넬을 만들고 싶습니다. [1] 원우아빠 2017.09.20 754
1025 XE7사용 블루투스 통신앱 안드로이드 롤리팝업그레이드 후 이상증세 [1] 아기사랑 2015.05.08 755
1024 앱설치시 홈화면에 바로가기 만들기 문의 [2] 김태윤 2016.04.07 755
1023 dephi 10.1 DBExpress MySQL 접속 문제 질문드립니다. [1] kevin 2020.11.19 755
1022 delphi 10 seattle 버전 아이폰 앱 컴파일 오류 [5] file 사랑남 2015.09.23 756
1021 BindingsList1 properties의 autobuffercount [1] silkroad99 2015.01.27 758
1020 델파이 Query.Reflesh와 DataSource.DataSet.ReFlesh의 차이점은? [1] 스카이 2018.07.31 759