[Home]  [Edit this page]  [Recent Changes]  [Special Pages]  [Help
CppVclTStringGrid

(C++ VCL) TStringGrid

Visual Component resembling a table for display and input.

Forward declaration:
namespace Grids { class TStringGrid; };


In the implementation (.cpp) file, #include grids.hpp:
  1. include <grids.hpp>


How to: add a TComboBox to a TStringGrid

Modified from the Borland community, from the Delphi code at http://community.borland.com/article/0,1410,17434,00.html

__fastcall TForm1::TForm1(TComponent* Owner)
  : TForm(Owner)
{
  //The combobox height is not settable, so we will 
  //instead size the grid to fit the combobox! 
  StringGrid1->DefaultRowHeight = ComboBox1->Height; 
  //Hide the combobox 
  ComboBox1->Visible = False; 
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ComboBox1Change(TObject *) 
{ 
  //Get the ComboBox selection and place in the grid 
  //Note that the lines below are NOT bulletproof. 
  //With some heavy clicking you can change Row and Col 
  //so it writes to another cell 
  if (StringGrid1->Col !=3 ) 
  { 
    StringGrid1->Col = 3; 
    ShowMessage("Prevention!"); 
  } 
  if (StringGrid1->Row <1 ) 
  { 
    StringGrid1->Row = 1; 
    ShowMessage("Prevention!"); 
  }
  StringGrid1->Cells[StringGrid1->Col][StringGrid1->Row] =
    ComboBox1->Items->operator [](ComboBox1->ItemIndex); 
  ComboBox1->Visible = False; 
  StringGrid1->SetFocus(); 
}
//---------------------------------------------------------------------------
void __fastcall TForm1::StringGrid1SelectCell(
  TObject *, int ACol, int ARow, bool &CanSelect) 
{ 
  if (ACol == 3 && ARow > 0) 
  { //Size and position the combo box to fit the cell 
    TRect R = StringGrid1->CellRect(ACol, ARow); 
    R.Left   += StringGrid1->Left; 
    R.Right  += StringGrid1->Left; 
    R.Top    += StringGrid1->Top; 
    R.Bottom += StringGrid1->Top; 
    ComboBox1->Left = R.Left + 1; 
    ComboBox1->Top = R.Top + 1; 
    ComboBox1->Width = (R.Right + 1) - R.Left; 
    ComboBox1->Height = (R.Bottom + 1) - R.Top; 
    //Show the combobox 
    ComboBox1->Visible = true; 
    ComboBox1->SetFocus(); 
  } 
  CanSelect = true; 
}


How to: let a StringGrid size itself to the text it contains?

void autoSize(TStringGrid * stringGrid)
{
  const int nCols = stringGrid->ColCount;
  const int nRows = stringGrid->RowCount;
  int sumWidth = 0;
  for (int x=0; x!=nCols; ++x)
  {
    int maxWidth = -1;
    for (int y=0; y!=nRows; ++y)
    {
      const int width = stringGrid->Cells[x][y].Length() * 8 + 5;
      if (width > maxWidth)
      {
        maxWidth = width;
      }
    }
    stringGrid->ColWidths[x] = maxWidth;
    sumWidth +=maxWidth;
  }
  stringGrid->Width = sumWidth + 7;
}


How to: Sort a StringGrid ?

Here's an implementation assuming the StringGrid is filled with integers.

void swapRows(TStringGrid * const stringGrid,const int& row1,const int& row2)
{
  const int nCols = stringGrid->ColCount;
  for (int j=0; j<nCols; ++j)
  {
    const String t = stringGrid->Cells[j][row1];
    stringGrid->Cells[j][row1] = stringGrid->Cells[j][row2];
    stringGrid->Cells[j][row2] = t;
  }
}
void sortStringGrid(TStringGrid * const grid, const int& col)
{
  const int fixedRows = grid->FixedRows;
  const int nRows     = grid->RowCount;
  //Sort the StringGrid
  for (int i=fixedRows; i!=nRows-1; ++i)
  {
    for (int y=fixedRows; y!=nRows-1; ++y)
    {
      const String upper = grid->Cells[col][y  ];
      const String lower = grid->Cells[col][y+1];
      if (upper.ToDouble() < lower.ToDouble()) { swapRows(grid,y,y+1); }
    }
  }
}


Convert a TStringGrid to a matrix

The matrix is implemented by a two dimensional std::vector.

  1. include <cassert>
std::vector<std::vector<double> > Convert(const TStringGrid * const stringGridOriginal) { //Need to cast away const as the VCL is not const-correct. Grumble, grumble... TStringGrid * const stringGrid = const_cast<TStringGrid*>(stringGridOriginal); assert(stringGrid!=0); assert(stringGrid->FixedCols == 0); assert(stringGrid->FixedRows == 0); const int maxx = stringGrid->ColCount; const int maxy = stringGrid->RowCount; std::vector<std::vector<double> > matrix(maxx,std::vector<double>(maxy,0.0)); for (int y=0; y!=maxy; ++y) { for (int x=0; x!=maxx; ++x) { const String myString = stringGrid->Cells[x][y]; double rDouble = 0.0; const bool success = TryStrToFloat(myString, rDouble); assert(success == true); matrix[x][y] = rDouble; } } return matrix; }


Code links

'TStringGrid' links



last edited (December 20, 2006) by bilderbikkel, Number of views: 3631, Current Rev: 7 (Diff)

[Edit this page]  [Page history]  [What links here]  [Discuss this topic]  [Printer Friendly]  

Members

Username:

Password:


Register
Forgot Password?




Programmers Heaven - for .NET, Java, C/C++ and WEB Developers!
© 1996-2008 Community Networks Ltd. All rights reserved. Reproduction in whole or in part, in any form or medium without express written permission is prohibited. Violators of this policy may be subject to legal action. Please read Terms Of Use and Privacy Statement for more information. Development by Tore Nestenius at .NET Consultant - Synchron Data.