views:

20

answers:

0

For C# and Windows Forms the actual needed width in pixels can be calculated as such:

Edit: Using col.Visible instead of col.Displayed as per smirkingman's comment!

public int getDisplayingWidth( DataGridView gv )
{
    int gvWidth = 0;

    if ( gv.RowHeadersVisible )
    {
        gvWidth += gv.RowHeadersWidth;
    }

    if ( ( gv.ScrollBars == ScrollBars.Vertical )
        || gv.ScrollBars == ScrollBars.Both )
    {
        gvWidth += System.Windows.Forms.SystemInformation.VerticalScrollBarWidth;
    }

    foreach ( DataGridViewColumn col in gv.Columns )
    {
        if ( col.Visible )
        {
            gvWidth += ( col.Width + col.DividerWidth + 1 );
        }
    }

    return gvWidth;
}

The +1 in the loop seems to be necessary, this seems to be the delimiter (although I would have thought that this is taken into account with DividerWidth...).

I found pieces of the solution. but not the complete solution, so I thought I just post it!

hth

Mario