3
gridView.FirstDisplayedScrollingRowIndex = gridView.SelectedRows[0].Index;

blowing up with:

No room is available to display rows

DataGridView is customized control in the unbound mode. datagridview.FirstDisplayedScrollingRowIndex is not set in a specific datagridview based event.

Rows are added to the datagridview via gatagridview.Rows.Add method on DataSet.EndMerge then cells are styled and formatted row by row. Last selected row is cleared and restored and scroll bar position restored to the first visible row.

Trying find out what this exception actually means.

if (gridView.Rows.Count > 0)
{
    gridView.ClearSelection();

    T value = GetItemByRow(gridView.Rows[0]);

    bool isVisible = filter.ShouldShow(value);
    gridView.Rows[0].Selected = true;

    if (!isVisible)
    {
        gridView.Rows[0].Visible = true;
        gridView.FirstDisplayedScrollingRowIndex = gridView.SelectedRows[0].Index; 
        gridView.Rows[0].Visible = false;
    }
    else
    {
        gridView.FirstDisplayedScrollingRowIndex = gridView.SelectedRows[0].Index;
    }
}
Boris Kleynbok
  • 317
  • 2
  • 5
  • 15

2 Answers2

1

When the grid is automatically sized to be 0 height (or probably width) and you set

dataGridView.FirstDisplayedScrollingRowIndex = 0;

the exception will be thrown. Our grid has set Dock = Fill and in some cases was sized to be invisible.

Our fix was to set a MinimumSize to 100/50, and we never saw the exception again. Even if the grid is not within the window (and thus invisible) the exception will now not be thrown.

Michael
  • 386
  • 2
  • 9
-1

When are row is present in the grid but not visible setting it to be first row will make it throw this exception.

Boris Kleynbok
  • 317
  • 2
  • 5
  • 15