자유롭게 질의 및 응답을 할 수 있는 게시판입니다. 개발자 여러분의 답변이 큰 도움이 됩니다. 
  • 제품설치/등록 오류 문의: 설치/등록 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 17174
공지 유용한 관련 사이트 관리자2 2014.03.20 56042
공지 본 게시판은 개발자 여러분들의 질문과 답변을 공유하는 공간입니다. 관리자 2012.01.10 99476
2239 델파이 XE CODE 1400 에러문의 [1] file 후두리찹찹 2018.03.06 235
2238 사용자가 3명 정도인데, 2티어로 모바일앱을 구성해도 괜찮을까요 ? [1] 막스뮐러 2020.04.29 235
2237 IBConsole에서 [3] file 개발자로 2018.09.07 235
2236 바탕화면 bds 실행 아이콘 이미지가 안보입니다. [1] file 거북선생 2019.10.07 235
2235 IOS 새로고침 드래그시 이슈 delphiman 2021.05.06 235
2234 VCL 소스 폴더와 ShellControls 컴포넌트가 왜 없나요? (XE10) [1] 서어 2016.05.04 236
2233 델파이 컴파일 후 실행 오류 관련 [2] file 똘귀아빠 2020.01.31 236
2232 "D3D_SHADER_MACRO" 에러가 발생 합니다. [2] 하루 2020.06.10 236
2231 [베를린] 신규 프로젝트에서 버튼하나 놓고 휴대폰으로 배포하면 오류가 납니다. 델사냥 2016.07.14 236
2230 10 SEATLE 평가판 사용하고 있습니다. socket 설치 문의드려요. [1] 영스 2016.02.29 236
2229 [REST 웨비나] REST API와 기존 SOAP와 차이점이 뭔가요? 험프리 2020.09.17 236
2228 델파이 xe7 로딩시 오류 [1] file chchchchchch 2018.01.04 237
2227 FireDAC. FDMEMTable의 txt 반출관련 속도개선 문의드립니다. [4] 오바라미 2018.10.16 237
2226 Uses Permissions Target [3] file 헌터 2018.12.06 237
2225 fast report 재설치 문의 [3] sub3 2020.02.28 237
2224 macOS Big Sur 에서 [DCC Error] E2597 Undefined symbols for architecture arm64: 에러 나옵니다. file 텔로스김 2020.12.17 237
2223 [시애틀 출시세미나] 최근 나온 델파이 기술서적(문법과 컴포넌트 소개)을 알려주세요. Humphery 2015.09.11 238
2222 델파이10 시애틀에서 TFDIBBackup에서 한글 DB명 사용 질문드립니다. [1] 불나방 2016.04.27 238
2221 엑셀 내보내기 버전지 [1] 그몽이 2019.06.12 238
2220 [XE8 웹세미나] 웹 환경의 업무 프로젝트 개발이 얼마나 편리한가요? Humphery 2015.05.13 238