TStringGrid에는
Row나 Column을 insert하거나 Delete하는 method를 제공하지 않는다..
대개 insert 를 하는경우 RowCount를 하나 증가하고
insert 한 row 부터 끝까지 하나씩 뒤로 뒤로 옮기는 것으로 하는것 같다.
뭐 같은 원리지만..
TCustomGrid 의 procted 함수에 보면 MoveRow , MoveColumn이라는 함수를 제공한다.
이 함수는 특정index의 row를 원하는 곳으로 이동시키고 ...
그사이에 row들을 하나씩 전체 밀어준다.
이미 만들어져있는 TCustomGrid 의 MoveRow , MoveColumn 함수를 이용하여
InsertRow , DeleteRow , InsertColumn , DeleteColumn을 만들어서 사용하면 더욱깔끔할것 같다.
class TMyGrid : public TStringGrid
{
public:
void __fastcall InsertRow( int idx)
{
this->RowCount=this->RowCount+1;
this->MoveRow(this->RowCount-1,idx);
}
void __fastcall DeleteRow(int idx)
{
this->MoveRow(idx,this->RowCount-1);
this->RowCount=this->RowCount-1;
}
void __fastcall ChangeRow(int idx1,int idx2)
{
TStringList *lst=new TStringList;
lst->Assign(this->Rows[idx1]);
this->Rows[idx1]=this->Rows[idx2];
this->Rows[idx2]=lst;
delete lst;
}
void __fastcall InsertCol(int idx)
{
this->ColCount=this->ColCount+1;
this->MoveColumn(this->ColCount-1,idx);
}
void __fastcall DeleteCol(int idx)
{
this->MoveColumn(idx,this->ColCount-1);
this->ColCount=this->ColCount-1;
}
};
//---------------------------------------------------------------------------
사용
//Row Insert
((TMyGrid*)StringGrid1)->InsertRow(2);
//Row Delete
((TMyGrid*)StringGrid1)->DeleteRow(3);
//Column Insert
((TMyGrid*)StringGrid1)->InsertCol(4);
//Column Delete
((TMyGrid*)StringGrid1)->DeleteCol(5);
..
참조
http://www.delphi.co.kr/zboard/view.php?id=tips&page=78&sn1=&divpage=1&sn=off&ss=on&sc=on&select_arrange=headnum&desc=asc&no=207
추신 2010-03-30
//일부 델파이 버젼을 추가합니다.
type
TMyGrid = class(TStringGrid)
public
procedure InsertRow(idx: Integer);
procedure DeleteRow(idx: Integer);
procedure ChangeRow(idx1,idx2:Integer);
end;
var
Form1: TForm1;
implementation
{ TMyGrid }
procedure TMyGrid.InsertRow(idx: Integer);
begin
self.RowCount:=self.RowCount+1;
self.MoveRow(self.RowCount-1,idx);
end;
procedure TMyGrid.DeleteRow(idx: Integer);
begin
self.MoveRow(idx,self.RowCount-1);
self.RowCount:=self.RowCount-1;
end;
procedure TMyGrid.ChangeRow(idx1, idx2: Integer);
var
lst:TStringList;
begin
lst:=TStringList.Create;
lst.Assign(self.Rows[idx1]);
self.Rows[idx1]:=self.Rows[idx2];
self.Rows[idx2]:=lst;
lst.free;
end;
그럼..
|
DeleteRow, DeleteColumn 으로 구현되있습니다.