Q&A

  • stringgrid에서 정렬하는예좀 알려주십시요..제발
델파이 시작한지 얼마안되는 초보자입니다..

비주얼 베이직만 하다가 델파이를 시작했는데

스트링 그리드 소트하는 법이 나와있는 책들을

찾을수가 없어서요..

제발 스트링그리드에서 소트하는 예좀 알려주세요..

예로 오름차순과 내림차순으로요..

제발 부탁합니다..

2  COMMENTS
  • Profile
    씨나락 2001.12.03 06:58
    다음과 같이 하면 될것 같네요

    인터넷에서 찾은 팁입니다..

    어딘지 북마크 해놓을걸

    쩝....



    ^.^;





    type

    { We will need tmpGrid to typcast our StringGrid1 to

    gain access to the protected TCustomGrid function

    TCustomGrid.MoveRow }

    tmpGrid = class(TCustomGrid);



    TForm1 = class(TForm)

    StringGrid1: TStringGrid;

    procedure FormCreate(Sender: TObject);

    procedure StringGrid1DblClick(Sender: TObject);

    private

    { Private declarations }

    public

    { Public declarations }

    end;



    {...}



    procedure TForm1.FormCreate(Sender: TObject);

    begin

    { set our stringgrid to allow editing }

    StringGrid1.Options := StringGrid1.Options + [goEditing];



    { set stringgrid1 to a better height then it's

    normal default }

    StringGrid1.DefaultRowHeight := 20;



    { remove fixed cols }

    StringGrid1.FixedCols := 0;



    { set column count }

    StringGrid1.ColCount := 3;



    { set column headers }

    StringGrid1.Cells[0,0] := 'First Name';

    StringGrid1.Cells[1,0] := 'Last Name';

    StringGrid1.Cells[2,0] := 'Department';

    end;



    procedure TForm1.StringGrid1DblClick(Sender: TObject);

    var

    x, Counter, SortColumn : integer;

    tmpPoint : TPoint;

    tmpCoor : TGridCoord;

    begin

    { get our cursor position on the screen when we

    double-click }

    GetCursorPos(tmpPoint);



    { get our projects cursor position relative to the

    screen position }

    tmpPoint := StringGrid1.ScreenToClient(tmpPoint);



    { get our grid coord from the client cursor position }

    tmpCoor := StringGrid1.MouseCoord(tmpPoint.X,tmpPoint.Y);



    { only continue if the header row was double-clicked }

    if tmpCoor.y = 0 then

    SortColumn := tmpCoor.X

    else

    Exit;



    { counter is used to get us out of the repeat }

    Counter := 0;



    repeat

    inc(Counter);



    { start x at 1 to skip our header column }

    for x := 1 to StringGrid1.RowCount - 2 do

    { compare rows }

    if CompareStr(StringGrid1.Cells[SortColumn,x],

    StringGrid1.Cells[SortColumn,(x+1)]) > 0 then

    { type-cast StringGrid1 with a TCustomGrid class

    to gain access to the MoveRow protected function }

    tmpGrid(StringGrid1).MoveRow(x+1,x);



    { change Counter=1000 if you have a super large grid }

    until (Counter = 1000);



    { force StringGrid1 to repaint our changes }

    StringGrid1.Repaint;

    end;



    {...}





    Example 2



    {...}



    type

    { We will need tmpGrid to typcast our StringGrid1 to

    gain access to the protected TCustomGrid function

    TCustomGrid.MoveRow }

    tmpGrid = class(TCustomGrid);



    TForm1 = class(TForm)

    StringGrid1: TStringGrid;

    procedure FormCreate(Sender: TObject);

    procedure StringGrid1DblClick(Sender: TObject);

    procedure TForm1.StringGrid1MouseDown(Sender: TObject;

    Button: TMouseButton;

    Shift: TShiftState;

    X, Y: Integer);

    private

    { Private declarations }

    tmpRow,tmpCol : Integer;

    public

    { Public declarations }

    end;



    {...}



    procedure TForm1.FormCreate(Sender: TObject);

    begin

    { set our stringgrid to allow editing }

    StringGrid1.Options := StringGrid1.Options + [goEditing];



    { set stringgrid1 to a better height then it's

    normal default }

    StringGrid1.DefaultRowHeight := 20;



    { remove fixed cols }

    StringGrid1.FixedCols := 0;



    { set column count }

    StringGrid1.ColCount := 3;



    { set column headers }

    StringGrid1.Cells[0,0] := 'First Name';

    StringGrid1.Cells[1,0] := 'Last Name';

    StringGrid1.Cells[2,0] := 'Department';

    end;



    procedure TForm1.StringGrid1MouseDown(Sender: TObject;

    Button: TMouseButton;

    Shift: TShiftState;

    X, Y: Integer);

    begin

    StringGrid1.MouseToCell(X,Y,tmpCol,tmpRow);

    end;



    procedure TForm1.StringGrid1DblClick(Sender: TObject);

    var

    x, Counter, SortColumn : integer;

    begin



    { only continue if the header row was double-clicked }

    if tmpRow = 0 then

    SortColumn := tmpCol

    else

    Exit;



    { counter is used to get us out of the repeat }

    Counter := 0;



    repeat

    inc(Counter);



    { start x at 1 to skip our header column }

    for x := 1 to StringGrid1.RowCount - 2 do

    { compare rows }

    if CompareStr(StringGrid1.Cells[SortColumn,x],

    StringGrid1.Cells[SortColumn,(x+1)]) > 0 then

    { type-cast StringGrid1 with a TCustomGrid class

    to gain access to the MoveRow protected function }

    tmpGrid(StringGrid1).MoveRow(x+1,x);



    { change Counter=1000 if you have a super large grid }

    until (Counter = 1000);



    { force StringGrid1 to repaint our changes }

    StringGrid1.Repaint;

    end;





    대헌욱 wrote:

    > 델파이 시작한지 얼마안되는 초보자입니다..

    > 비주얼 베이직만 하다가 델파이를 시작했는데

    > 스트링 그리드 소트하는 법이 나와있는 책들을

    > 찾을수가 없어서요..

    > 제발 스트링그리드에서 소트하는 예좀 알려주세요..

    > 예로 오름차순과 내림차순으로요..

    > 제발 부탁합니다..

  • Profile
    대헌욱 2001.12.03 21:59
    정말 고맙습니다. 씨나락님^^

    정말 많은 도움이 됐습니다..

    이은혜 어떻게 보답해야될지..^^;

    그럼 앞으로도 많은 관심 부탁드립니다..

    그럼 즐거운 하루되십시요..