Mega Code Archive

 
Categories / Delphi / Algorithm Math
 

TStringGrid functions (Delete, Insert, Sort)

Title: TStringGrid functions (Delete, Insert, Sort) Question: If you need to delete a specific column from a StringGrid you can use the following procedure: Answer: Procedure GridRemoveColumn(StrGrid: TStringGrid; DelColumn: Integer); Var Column: Integer; begin If DelColumn Begin For Column := DelColumn To StrGrid.ColCount-1 do StrGrid.Cols[Column-1].Assign(StrGrid.Cols[Column]); StrGrid.ColCount := StrGrid.ColCount-1; End; end; This routine was provided by Norbert Landa Martnez. Procedure GridAddColumn(StrGrid: TStringGrid; NewColumn: Integer); Var Column: Integer; begin StrGrid.ColCount := StrGrid.ColCount+1; For Column := StrGrid.ColCount-1 downto NewColumn do StrGrid.Cols[Column].Assign(StrGrid.Cols[Column-1]); StrGrid.Cols[NewColumn-1].Text := ''; end; Procedure GridSort(StrGrid: TStringGrid; NoColumn: Integer); Var Line, PosActual: Integer; Row: TStrings; begin Renglon := TStringList.Create; For Line := 1 to StrGrid.RowCount-1 do Begin PosActual := Line; Row.Assign(TStringlist(StrGrid.Rows[PosActual])); While True do Begin If (PosActual = 0) Or (StrToInt(Row.Strings[NoColumn-1]) = StrToInt(StrGrid.Cells[NoColumn-1,PosActual-1])) then Break; StrGrid.Rows[PosActual] := StrGrid.Rows[PosActual-1]; Dec(PosActual); End; If StrToInt(Row.Strings[NoColumn-1]) StrGrid.Rows[PosActual] := Row; End; Renglon.Free; end; This routine was provided by Norbert Landa Martnez.