如何自动调整DBGrid列的宽度

时间:2013-02-22 08:59:45

标签: delphi c++builder tdbgrid c++builder-xe

我一直尝试在C ++ Builder XE中使用 this Delphi code 。我手动将语言转换为C ++ Builder语言。

此过程正常运行。当我尝试最大化持有DBGrid的表单时,此过程根据网格宽度成功分割列宽。然而, 如果我尝试使用窗体右侧或左侧的鼠标控件来最小化窗体宽度,则此过程不会将列宽调整为值的长度或每列上标签的长度。

有什么办法可以解决吗?或者任何人都有另一个程序来解决这样的问题?

编辑:好的,这是代码。我已经手动将上面的代码转换为此代码:

void __fastcall TfmMain::FixGridColumnsWidth(TDBGrid *DBGrid)
{
int       TotalColumnWidth, ColumnCount, GridClientWidth, Filler, i;


    ColumnCount = DBGrid->Columns->Count;

    if  (ColumnCount == 0)
        exit;

    // compute total width used by grid columns and vertical lines if any
    TotalColumnWidth = 0;

    for ( i = 0; i <=ColumnCount-1; i++)

        TotalColumnWidth = TotalColumnWidth + DBGrid->Columns->Items[i]->Width;
        if ( DBGrid->Options.Contains(dgColLines) )

        // 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 = DBGrid->Width - GetSystemMetrics(SM_CXVSCROLL);

    if ( DBGrid->Options.Contains(dgIndicator) )  {
        GridClientWidth = GridClientWidth - IndicatorWidth;
        if ( DBGrid->Options.Contains(dgColLines) )
            GridClientWidth--;
    }

    if ( DBGrid->BorderStyle == bsSingle )  {
        if ( DBGrid->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 =  (GridClientWidth - TotalColumnWidth) / ColumnCount;
        for ( i = 0; i <= ColumnCount-1; i++ )
            DBGrid->Columns->Items[i]->Width = DBGrid->Columns->Items[i]->Width + Filler;
    }

    else
        if ( TotalColumnWidth > GridClientWidth ) {
            Filler = (TotalColumnWidth - GridClientWidth) / ColumnCount;
            if ( (TotalColumnWidth - GridClientWidth) % ColumnCount != 0 )
                Filler++;
            for ( i = 0; i <= ColumnCount-1; i++ )
                DBGrid->Columns->Items[i]->Width = DBGrid->Columns->Items[i]->Width - Filler;
        }


}

提前致谢。

0 个答案:

没有答案