0

I am trying to create a drop down borderless form for my custom combo box. The drop down form includes a grid that the user can select items from. For the most part, it works. But, I've noticed there is space at the bottom of the grid that does not contain any data. I have set AllowUserToAddRows to false. The extra space doesn't even include cells anyway--its just blank control space. Just to make sure that the extra space was in the grid (and not the form), I set the grid BackgroundColor to yellow and it shows up in the form, so I know the extra space is in the grid.

Note: I am adding the grid to the form programmatically. I'm not sure if that matters. And, I'm resizing the form based on the grid size. The form width is set to the grid width. If the grid contains more than 10 rows, I set the form height to show only 10 rows and the vertical scroll bar allows the user to scroll. If the grid contains less than 10 rows, I set the form to fit those rows exactly and no vertical scroll bar is needed. Does any of this sound like it would affect the grid size?

I'm doing the following in the form's VisibleChanged event to resize the form based on grid contents. Note that this could have typos since I retyped it from a printout. Note: MaxRowsToShow = 10.

int rowsHeight = _grid.Rows.GetRowsHeight(DataGridViewElementStates.Visible);
int rowCount = _grid.Rows.GetRowCount(DataGridViewElementStates.Visible);
int rowHeight = _grid.RowTemplate.Height;
int rowsToShow = Math.Min(MaxRowsToShow, rowCount);
Height = rowsToShow * rowHeight + _grid.ColumnHeadersHeight;

UPDATE: I made some progress. I found out that setting the DataGridView's ScrollBars property to ScrollBars.Vertical was causing the extra space at the end of the grid. If I set ScrollBars to ScrollBars.None when I have less that 10 items, I do not see the extra space. But, I still need the verticall scroll bars when I have more than 10 items, so I'm still seeing the extra space in that case. But, at least I figured out the source of the problem. Does anyone know why the ScrollBars property is causing this weird behavior.

bsh152s
  • 3,178
  • 6
  • 51
  • 77
  • Are there less than 10 rows when this happens? – Igby Largeman Nov 17 '11 at 16:03
  • How do you calculate the form height to show only 10 rows? – Igby Largeman Nov 17 '11 at 16:05
  • @Charles, this happens when there are more than ten rows or less than ten rows. Whenever there is less than ten rows, I would expect the vertical scroll bars to not exist, but they do because of the extra space at the bottom. I've added code to my question. Although I did not include the width changes, but I don't think that would affect this problem. – bsh152s Nov 17 '11 at 16:19
  • I do believe the form is being size correctly to the populated rows. But, there is always extra space at the bottom about the height of a row. I only mentioned the sizing of the form because I didn't know if it would somehow affect the size of the child grid. – bsh152s Nov 17 '11 at 16:28
  • See: http://stackoverflow.com/questions/2122985/removing-the-empty-gray-space-in-datagrid-in-c-sharp – Tom Bogle Aug 22 '16 at 15:40

2 Answers2

0

Did you tried attributes :

VerticalAlignment="Stretch"
HorizontalAlignment="Stretch"

?

Guillaume Slashy
  • 3,554
  • 8
  • 43
  • 68
0

Do you basically have a user control or form that should be the same size as the grid?

If so, set the form's size. Any control using it can then size it however it wants but it has a size for layout. Then put a container control on the form and set the container (such as a panel) to Dock -> Fill. Then put the grid on that container and also set it to Dock -> Fill. Then stop trying to set the height and stuff of the grid. It will have whatever room its container allows it to have.

Winforms is meant to have controls inherit their parents' widths and heights and be sized to fit in them; by resetting the form to match the grid, it's sort of backwards. The user (or program) should resize the form, and the grid will size along with it.

If the grid shouldn't take up the entire form, you can just split the container and have the grid Anchor to top/left/right (for top fill) instead of setting the Dock property.

Nikki9696
  • 6,260
  • 1
  • 28
  • 23
  • The above code is setting the form size. I have set the grid's Dock property to fill. I am not manually setting the grid size. – bsh152s Dec 06 '11 at 16:38