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.