자유롭게 질의 및 응답을 할 수 있는 게시판입니다. 개발자 여러분의 답변이 큰 도움이 됩니다. 
  • 제품설치/등록 오류 문의: 설치/등록 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 15775
공지 유용한 관련 사이트 관리자2 2014.03.20 54595
공지 본 게시판은 개발자 여러분들의 질문과 답변을 공유하는 공간입니다. 관리자 2012.01.10 98015
499 [세미나] 스플래쉬 추가후 일부 구동시 문제가 발생합니다. [1] Humphery 2014.06.12 2050
498 combobox 로 lookup 구현방법 [1] file 정헌이 2014.06.04 2059
497 [공유] 웹뷰를 이용한 하이브리드 앱 개발 시 궁금한 점 험프리 2017.08.02 2062
» [공유] 스트링그리드(TStringGrid)를 정렬하고 싶어요. file 험프리 2015.10.27 2066
495 문자 수신화면에서 url링크를 클릭하면 자동실행 [1] superman 2015.11.10 2068
494 [문의]android target에 다른 pc emulator 추가 하는방법 [1] 강성범 2014.09.02 2072
493 [질문] DataSnap에서 메시지 전달방법 [1] cdrd 2014.03.25 2079
492 [세미나] 네이티브코드로 개발된 모바일 버전과 파일사이즈가 차이가 납니다. [1] Humphery 2014.06.12 2084
491 [XE6] Baas 푸시 세미나에 관련된 궁금한 질문입니다. [1] 가을이다 2014.08.20 2089
490 FireMonkey Form을 Win32 Panel에 올릴 수 있는 방법이 있는지요. [2] 늦바람 2014.06.10 2093
489 [XE6]스플래쉬 화면 구성시 [1] 가을이다 2014.08.13 2096
488 XE7에서 FireDAC를 이용하여 AccessDB사용시 배포 [3] 투덜이스머프 2014.12.10 2096
487 [세미나] 휴대폰번호 인증서비스도 지원하나요? [1] Humphery 2014.07.30 2098
486 [마이그레이션][64bit] 64비트에서 CreateFileMapping의 반환값(핸들)이 0으로 표시됩니다. 험프리 2017.01.31 2101
485 [세미나] C++빌더 XE7에 자바 라이브러리 호출이 가능한가요? [1] Humphery 2014.09.17 2110
484 XE6, XE7 (주)머든 2014.11.03 2112
483 XE5 스크롤시 버벅현상 [1] 가을이다 2014.06.20 2115
482 c++ builderXE6 UPGRADE Code Insight 기능 관련 [3] 껌돌 2014.06.30 2118
481 델파이와 오라클 호환성 [1] 오라클매니아 2014.03.06 2124
480 Delphi XE7 kbmmemtable (CodeGear Edition) 설치 오류 [1] 도사 2014.10.06 2131