By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,175 Members | 1,767 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,175 IT Pros & Developers. It's quick & easy.

DataGridView - Vertical ScrollBar Help Needed

P: n/a
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[i].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
Jan 30 '08 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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.
--
Misbah Arefin

"Tom" wrote:
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[i].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
Feb 3 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.