Setting DataGridView.Height to Goldilocks value?

  • Thread starter Thread starter eBob.com
  • Start date Start date
E

eBob.com

How do I set the Height of a DataGridView to just accomodate what I've put
in it? Not too big, not too small.

I looked for a property which I could set such that the Height would grow as
I added rows. But found no such property.

So I thought, OK, I can calculate the value, after populating the
DataGridView, like this ...

MasterSegs(cmsi).Height = (MasterSegs(cmsi).Rows.Item(0).Height *
MasterSegs(cmsi).Rows.Count) _
+ MasterSegs(cmsi).ColumnHeadersHeight

(All rows are the same height and there is always at least one row.)

But that creates extra space within the control but beyond the last row. In
one case I was able to set exactly the right size by getting rid of the
vertical scroll bar. But in another case getting rid of the vertical scroll
bar made no difference - there is still unused space beyond the last row.

I even tried subtracting 1 and even 2 from the Rows.Count but that didn't
seem to make any difference.

This is really annonying as I am trying to layout consecuitive DataGridViews
in a Panel and I don't know how to specify the Y locations of the
DataGridViews.

My searches found that this problem comes up from time to time but I have
not found a solution posted.

Thanks, Bob
 
Hi Bob,

Are you aware that there are no 80*25 rows monitors anymore.

The way something is showed is completely depended from the setting form the
user.

My experience (but you can ignore that) is the more I do to make it look
nice on my screen, the more it looks awfully on other screens

Cor
 
eBob.com said:
How do I set the Height of a DataGridView to just accomodate what I've put
in it? Not too big, not too small.

I looked for a property which I could set such that the Height would grow
as I added rows. But found no such property.

So I thought, OK, I can calculate the value, after populating the
DataGridView, like this ...

MasterSegs(cmsi).Height = (MasterSegs(cmsi).Rows.Item(0).Height *
MasterSegs(cmsi).Rows.Count) _
+ MasterSegs(cmsi).ColumnHeadersHeight

(All rows are the same height and there is always at least one row.)


I'm pretty sure you need to handle the datagridview yourself, as there is
likly no sizing mode to do what you want. You seem to only be changing the
control height based on content, not the cell height. Given that, you will
likely run out of real estate on the form at some time.

But that creates extra space within the control but beyond the last row.
In one case I was able to set exactly the right size by getting rid of the
vertical scroll bar. But in another case getting rid of the vertical
scroll bar made no difference - there is still unused space beyond the
last row.

Are you calling your resize routine at startup? I don't see why this would
be seen
I even tried subtracting 1 and even 2 from the Rows.Count but that didn't
seem to make any difference.

Try subtracting 2 pixels * the number of rows. It worked for me. My guess
is the height includes the borders, which are off by one because the cells
don't repeat the border if one is next to it.

You are not docking or anchoring the control, are you?
This is really annonying as I am trying to layout consecuitive
DataGridViews in a Panel and I don't know how to specify the Y locations
of the DataGridViews.

Don't dock or anchor the panel either.

The datagridview.Location is how you specify the location of the
datagridview.

In general though, I think using the scrollbars for a docked datagridview is
what users expect.
 
Back
Top