首页 > 解决方案 > 如何在datagridview的最后一行下获得灰色区域的高度?

问题描述

调整窗口大小时,我需要获得灰色区域的高度。

在此处输入图像描述

标签: datagridview

解决方案


This appears fairly straight forward.

  1. Create an (accumulator) variable totalDisplaySize to keep track of the total height of the “displayed” rows plus the column height.

  2. Start a loop at the “First Displayed Row Index” and end at the last row.

  3. Add each displayed row height to the accumulator totalDisplaySize.
  4. Return the grids height – (minus) totalDisplaySize.

Adding a check in the loop to see if the totalDisplaySize becomes greater than the grids height, may look something like…</p>

private int GetUnusedHeight() {
  int totalDisplaySize = dataGridView1.ColumnHeadersHeight;
  for (int i = dataGridView1.FirstDisplayedScrollingRowIndex; i < dataGridView1.RowCount; i++) {
    totalDisplaySize += dataGridView1.Rows[i].Height;
    if (totalDisplaySize >= dataGridView1.Height)
      return 0;
  }
  return dataGridView1.Height - totalDisplaySize;
}

推荐阅读