TDrawGrid i TStringGrid
6.08.1999
  • Wstawianie kolumn i wierszy do TStringGrid i ich usuwanie.

      Wiele operacji na komórkach zaimplementowanych przez TCustomGrid, z której wywodzi się TStringGrid, jest zabezpieczonych. Na szczęście, klasa TStrings, której przykładem są kolumny i wiersze TStringGrid, oferuje wiele użytecznych funkcji. Poniżej demonstruję dwie przykładowe funkcje wstawiające i kasujące kolumny z TStringGrid. Podobnie postąpić należy dla wierszy.

//--------------------------------------------------
//w nagłówku:
void __fastcall InsertCol(TStringGrid *StringGrid, long AfterIndex) ;
void __fastcall RemoveCol(TStringGrid *StringGrid, long Index);

//--------------------------------------------------
//w źródle:
//wstawia kolumnę

void __fastcall TForm1::InsertCol(TStringGrid *StringGrid,
                                          long AfterIndex) 
{ 
 StringGrid->ColCount = StringGrid->ColCount + 1;
  
 for (int col = StringGrid->ColCount - 1;
                            col > AfterIndex + 1; col--) 
    { 
        StringGrid->Cols[col] = StringGrid->Cols[col - 1]; 
    } 
    StringGrid->Cols[AfterIndex + 1]->Clear(); 
} 

//-----------------------------------------------------

//usuwa kolumnę 
void __fastcall TForm1::RemoveCol(TStringGrid *StringGrid,
                                               long Index) 
{ 
 for (int col = Index;
              col < StringGrid->ColCount - 1; col++) 
    { 
        StringGrid->Cols[col] = StringGrid->Cols[col + 1]; 
    } 
    StringGrid->ColCount = StringGrid->ColCount - 1; 
}