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

(C++ CLX) TStringGrid

Visual Component resembling a table for display and input.

Forward declaration:
namespace Qgrids { class TStringGrid; };


In the implementation (.cpp) file, #include QGrids.hpp:
  1. include <QGrids.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 * 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 * 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); }
    }
  }
}


Code links

TStringGrid Links



last edited (November 11, 2006) by bilderbikkel, Number of views: 1441, Current Rev: 3 (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.