DataGridView - Vertical ScrollBar Help Needed

T

Tom

First, I posted a similar request for help in another group and now
don't find the posting. Problem with my newsreader perhaps ... but
apologies if this appears as a cross posting.

My code is inconsistent in the detection of a vertical scrollbar. This
involves situations when less than the client area is needed to hold
the small amount of data. The inconsistency is when adjusting the
panel size within or just below the bottom row. Sometimes the code
adjusts the splitter of the panel as if a vertical scrollbar was
present ... when it is not. This results in seeing a portion of the
panel outside the DataGridView where the scrollbar resides when it is
needed.

I use the following code to detect if a vertical scrollbar is present:

grid2.AutoResizeColumns();
grid2.AutoResizeRows();

int i, sum = 0;
for(i = 0; i < grid2.ColumnCount; ++i)
{
sum + grid2.Columns.Width;
}

if(grid2.PreferredSize.Height > split2.Panel2.ClientSize.Height)
{
// Adjust splitter to fit columns WITH a vertical scrollbar present
split2.SplitterDistance =
Math.Max(100, ActiveForm.Width - sum + Split2.SplitterWidth - 35);
}
else
{
// Adjust to fit columns WITHOUT a vertical scrollbar present.
split2.SplitterDistance =
Math.Max(100, ActiveForm.Width - sum + Split2.SplitterWidth - 19);
}

I wish I knew how to directly determine if the vertical scrollbar is
present without using the PreferredSize.Height > ClientSize.Height
comparison. Rather than summing the column widths and using the sum
value, I've also used Preferred.Width ... but see the same effect.

I've printed out the Preferred.Width and ClientSize.Height and these
are updating correctly. The problem seems internal of the DataGridView
control itself in that it is obviously using a different determination
for the displaying of the scrollbar. I'm almost convinced this is
actually a flaw of the control that needs to be fixed ... but usually
when I feel this way I find it is because I have done something wrong
and the controls end up being robust.

Thanks for any help sent my way. :)

-- Tom
 
M

Misbah Arefin

if(dgv.DisplayRectangle.Height < dgv.ClientRectangle.Height)
//TODO: add code when scrollbar is showing
else
//TODO: add code when scrollbar is not showing

You will need to check the .RowHeight in the .RowTemplate and divide the
..DisplayRectangle by that to see how many rows will display, and if you have
more rows than that, the vertical scrollbar will show.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top