My DGV is populated with values entered by the user (not data-bound). The row count is dynamic. Even with the default five rows, though, tabbing from cell to cell and from row to row causes the first row (row 0) to disappear “up and under” the DGV when I get to the last (5th) row / row 4.
I want the 5 (or however many) rows to always stay in the same spot, rather than to move based on where the cursor is.
I don’t know why this is the default behavior in this scenario, and am curious about that, but mostly just need to know how to prevent this bizarreness.
When users add additional rows, I calculate the heights of each row like so:
dataGridViewPlatypi.Rows.Add();
int newRowHeight = dataGridViewPlatypi.Height / dataGridViewPlatypi.RowCount;
for (int i = 0; i < dataGridViewPlatypi.RowCount; i++)
{
dataGridViewPlatypi.Rows[i].Height = newRowHeight;
}
…this inexact math can lead to some “extra space” at the bottom of the grid when the division has a remainder/modulo, such as: When there are six rows, the height of each row is 26 and leaves 4 pixels over on the bottom of the grid — but again, this unnerving behavior even occurs when the default 5 rows are ensconced in the 160 pixel-high DGV (32 pixels per row).
I don’t know if I need to set some property or write some code for the DGV, a row, a cell, or…???
UPDATE
I’ve found that if I add 2 to the DGV’s height at design time (162 instead of 160, so that each row is 32 and there are 2 pixels “for the DGV” left over, it works with the default 5 rows.
HOWEVER, trying to get it to adjust when the user adds additional rows is proving problematic. The following kludgy code does NOT work:
// Adding one extra pixel is not enough; 2 is the least that can be added to prevent the last phantom grey row from appearing
const int BASE_GRID_HEIGHT_SHIM = 2;
const int DEFAULT_ROW_COUNT = 5;
dataGridViewPlatypi.Rows.Add();
int shimAdjustment = dataGridViewPlatypi.RowCount - DEFAULT_ROW_COUNT;
int newRowHeight = dataGridViewPlatypi.Height / dataGridViewPlatypi.RowCount;
int newGridHeight = (newRowHeight * dataGridViewPlatypi.RowCount) + BASE_GRID_HEIGHT_SHIM + shimAdjustment;
for (int i = 0; i < dataGridViewPlatypi.RowCount; i++)
{
dataGridViewPlatypi.Rows[i].Height = newRowHeight;
}
dataGridViewPlatypi.Size = new Size(dataGridViewPlatypi.Width, newGridHeight);
NOTE: The pushing up of the rows to hide row 0 and reveal the soft grey underbelly of the DGV does not occur when clicking in column 0; it is only after tabbing from one of the columns to the next column that this “up-pushing” occurs (FWIW).
UPDATE 2
This is how I applied the bountiful answer.
private void buttonAddRow_Click(object sender, EventArgs e)
{
AddAPlatypusRow();
for (int i = 0; i < dataGridViewPlatypi.RowCount; i++)
{
FreezeBand(dataGridViewPlatypi.Rows[i]);
}
}
Below is a complete sample on using DataGridView’s DataGridViewBand class to freeze any row or column.
In short, you just need:
Note: You can even freezed band’s BackColor as you wish. Although in the sample is WhiteSmoke(grey).
To see a complete demo look at Microsoft’s DataGridViewBandDemo
To see a fast demo, create a regular Visual Studio, C# Windows Application project and fill your Program.cs file with the following code.
Well, that is my best shot 🙂