Советы по Delphi

       

Вставка и удаление строк в StringGrid


...я не нашел никаких методов для вставки и удаления строк...

Поскольку свойство Cols[x] компонента TStringGrid реально является компонентом TStrings, все методы TStrings применимы также и к Cols[x].

Недавно в интернете я нашел реализацию расширенных функций TStringGrid:

Создано: Dennis Passmore 1929 Mango Tree Drive Edgewater, Fl. 32141 CIS: 71640,2464 Март 1, 1996 Данный код свободен в использовании при одном условии: в исходном коде должна присутствовать указанная выше кредитка со ссылкой на автора.

Примечание по использованию кода:
Всякий раз при удалении Row (строки) или Column (колонки) проверяйте наличие и удаляйте любые объекты, которые могли быть назначены любой ячейке в строке или колонке, которые вы собираетесь удалять, поскольку данный код не может знать ни размера, ни типа ассигнованных ими объектов.

*)


unit GridFunc;
interface
uses
Sysutils, WinProcs, Grids;
procedure InsertRow (Sender: TStringGrid; ToIndex: Longint);procedure DeleteRow (Sender: TStringGrid; FromIndex: Longint);procedure InsertColumn(Sender: TStringGrid; ToIndex: Longint);procedure DeleteColumn(Sender: TStringGrid; FromIndex: Longint);
implementation
type
TCSGrid = class(TStringGrid)privatepublicprocedure MoveRow (FromIndex, ToIndex: Longint);procedure MoveColumn(FromIndex, ToIndex: Longint);end;
procedure TCSGrid.MoveRow(FromIndex,ToIndex: Longint);beginRowMoved(FromIndex, ToIndex); { Защищенный метод TStringGrid }end;
procedure TCSGrid.MoveColumn(FromIndex, ToIndex: Longint);beginColumnMoved(FromIndex, ToIndex);{ Защищенный метод TStringGrid }end;
procedure InsertRow(Sender: TStringGrid; ToIndex: Longint);varxx,yy: Integer;beginif ToIndex>=0 thenwith TCSGrid(Sender) doif (ToIndex<=RowCount) thenbeginRowCount := RowCount + 1;xx := RowCount - 1;for yy := 0 to ColCount - 1 dobeginCells[yy,xx] := ' ';ObJects[yy,xx] := nil;end;if ToIndex<RowCount-1 thenMoveRow(RowCount-1,ToIndex);endelseMessageBeep(0)elseMessageBeep(0);end;
procedure DeleteRow(Sender: TStringGrid; FromIndex: Longint);beginif FromIndex>l;=0 thenwith TCSGrid(Sender) doif (RowCount>0)and(FromIndex<RowCount) thenbeginif (FromIndex<RowCount-1) thenMoveRow(FromIndex,RowCount-1);Rows[RowCount-1].Clear;RowCount := RowCount-1;endelseMessageBeep(0)elseMessageBeep(0);end;
procedure InsertColumn(Sender: TStringGrid; ToIndex: Longint);varxx,yy: Integer;beginif ToIndex>=0 thenwith TCSGrid(Sender) doif (ToIndex<=ColCount) thenbeginColCount := ColCount + 1;xx := ColCount - 1;Cols[xx].BeginUpdate;for yy := 0 to RowCount - 1 dobeginCells[xx,yy] := ' ';ObJects[xx,yy] := nil;end;Cols[xx].EndUpdate;if ToIndex<ColCount-1 thenMoveColumn(ColCount-1,ToIndex);endelseMessageBeep(0)elseMessageBeep(0);end;
procedure DeleteColumn(Sender: TStringGrid; FromIndex: Longint);beginif FromIndex>=0 thenwith TCSGrid(Sender) doif (ColCount>0)and(FromIndex<ColCount) thenbeginif (FromIndex<ColCount-1) thenMoveColumn(FromIndex,ColCount-1);Cols[ColCount-1].Clear;ColCount := ColCount-1;endelseMessageBeep(0)elseMessageBeep(0);end;
end.

- Dennis Passmore [000848]



Содержание раздела