Board index » cppbuilder » How to adjust TStringGrid column widths automatically ?

How to adjust TStringGrid column widths automatically ?


2005-08-05 09:57:58 PM
cppbuilder103
I use a Delphi procedure AdjustColumnWidths(DBGrid: TDBGrid) developed by
Philippe Randour (bdn.borland.com/article/0,1410,27548,00.html) as a
basis. My modified version for TStringGrid is below. I tried to find
C++Builder equivalents for StringGrid but it does not work properly. Could
somebody help ?
Pauli
/////////////////////////
void AdjustColumnWidths(TStringGrid& Grid)
{
int TotalColumnWidth, ColumnCount, GridClientWidth, Filler, i;
ColumnCount = Grid.ColCount;
if (ColumnCount == 0) return;
// compute total width used by grid columns and vertical lines if any
TotalColumnWidth = 0;
for (i = 0;i<ColumnCount;i++) TotalColumnWidth = TotalColumnWidth +
Grid.ColWidths[i]; // Delphi: DBGrid.Columns[i].Width;
if (Grid.Options.Contains(goVertLine))
{ // include vertical lines in total (one per column)
TotalColumnWidth = TotalColumnWidth + ColumnCount;
}
// compute grid client width by excluding vertical scroll bar, grid
indicator,
// and grid border
GridClientWidth = Grid.Width - GetSystemMetrics(SM_CXVSCROLL);
if (Grid.ScrollBars==ssVertical) //?? Delphi: if dgIndicator in
DBGrid.Options then
{
GridClientWidth = GridClientWidth - 0; // ?? Delphi: GridClientWidth :=
GridClientWidth - IndicatorWidth;
if(Grid.Options.Contains(goVertLine)) GridClientWidth=GridClientWidth-1;
}
if (Grid.BorderStyle == bsSingle)
{
if (Grid.Ctl3D)// border is sunken (vertical border is 2 pixels wide)
GridClientWidth = GridClientWidth - 4;
else // border is one-dimensional (vertical border is one pixel wide)
GridClientWidth = GridClientWidth - 2;
}
// adjust column widths
if (TotalColumnWidth < GridClientWidth)
{
Filler = (int)((GridClientWidth - TotalColumnWidth)/ColumnCount);
for (i = 0;i<ColumnCount;i++) Grid.ColWidths[i] = Grid.ColWidths[i] +
Filler;
}
else if (TotalColumnWidth>GridClientWidth)
{
Filler = (int)((TotalColumnWidth - GridClientWidth)/ColumnCount);
if (((TotalColumnWidth - GridClientWidth) % ColumnCount) != 0)
Filler=Filler+1;
for (i = 0;i<ColumnCount;i++)Grid.ColWidths[i] = Grid.ColWidths[i] -
Filler;
}
}
///////////////////////
 
 

Re:How to adjust TStringGrid column widths automatically ?

"Pauli" < XXXX@XXXXX.COM >wrote:
Quote
I use a Delphi procedure AdjustColumnWidths(DBGrid: TDBGrid) developed by
Philippe Randour (bdn.borland.com/article/0,1410,27548,00.html) as a
basis. My modified version for TStringGrid is below. I tried to find
C++Builder equivalents for StringGrid but it does not work properly. Could
somebody help ?

Pauli

/////////////////////////
void AdjustColumnWidths(TStringGrid& Grid)
{
int TotalColumnWidth, ColumnCount, GridClientWidth, Filler, i;
ColumnCount = Grid.ColCount;
if (ColumnCount == 0) return;

// compute total width used by grid columns and vertical lines if any
TotalColumnWidth = 0;
for (i = 0;i<ColumnCount;i++) TotalColumnWidth = TotalColumnWidth +
Grid.ColWidths[i]; // Delphi: DBGrid.Columns[i].Width;
if (Grid.Options.Contains(goVertLine))
{ // include vertical lines in total (one per column)
TotalColumnWidth = TotalColumnWidth + ColumnCount;
}

// compute grid client width by excluding vertical scroll bar, grid
indicator,
// and grid border
GridClientWidth = Grid.Width - GetSystemMetrics(SM_CXVSCROLL);
if (Grid.ScrollBars==ssVertical) //?? Delphi: if dgIndicator in
DBGrid.Options then
{
GridClientWidth = GridClientWidth - 0; // ?? Delphi: GridClientWidth :=
GridClientWidth - IndicatorWidth;
if(Grid.Options.Contains(goVertLine)) GridClientWidth=GridClientWidth-1;
}
if (Grid.BorderStyle == bsSingle)
{
if (Grid.Ctl3D)// border is sunken (vertical border is 2 pixels wide)
GridClientWidth = GridClientWidth - 4;
else // border is one-dimensional (vertical border is one pixel wide)
GridClientWidth = GridClientWidth - 2;
}

// adjust column widths
if (TotalColumnWidth < GridClientWidth)
{
Filler = (int)((GridClientWidth - TotalColumnWidth)/ColumnCount);
for (i = 0;i<ColumnCount;i++) Grid.ColWidths[i] = Grid.ColWidths[i] +
Filler;
}
else if (TotalColumnWidth>GridClientWidth)
{
Filler = (int)((TotalColumnWidth - GridClientWidth)/ColumnCount);
if (((TotalColumnWidth - GridClientWidth) % ColumnCount) != 0)
Filler=Filler+1;
for (i = 0;i<ColumnCount;i++)Grid.ColWidths[i] = Grid.ColWidths[i] -
Filler;
}
}
///////////////////////
Here's a quick method that works for me:
void __fastcall TForm1::FormShow(TObject *Sender)
{
TStringGrid *SG = new TStringGrid(this);
int TxtWidth;
SG->Parent = this;
SG->Align = alClient;
SG->ColCount = 2;
SG->FixedCols = 0;
SG->FixedRows = 0;
SG->Cols[0]->LoadFromFile("Col1.txt");
SG->Cols[1]->LoadFromFile("Col2.txt");
for(int i = 0; i < SG->ColCount; ++i)
for(int j = 0; j < SG->RowCount; ++j){
TxtWidth = Canvas->TextWidth(SG->Cells[i][j]);
if(TxtWidth>SG->ColWidths[i])
SG->ColWidths[i] = TxtWidth + 5;
}
}
//---------------------------------------------------------------------------