0

我在 DataGridView 中将 AllowUserToAddRows 设置为 false。然而,当我通过方向箭头键导航到最后一行时,顶行会滚动到看不见的地方,底部会出现一个新的灰色行。

我怎样才能防止这种情况?无论我将光标向上、向下、向左或向右移动多远,我都希望我的所有行都可见。

注意:水平浏览单元格不会导致问题 - 我这样做时没有添加灰色列。我希望行/垂直功能与此相同。

4

1 回答 1

1

So one approach to this would be do the following:

On the DataGridView, set the propertiesAllowUserToAddRows and AllowUserToDeleteRows to false

Also, set AutoSizeRowsMode to None

Handle the resize of the DataGridView like this:

private void dataGridView1_Resize(object sender, EventArgs e)
{
    var rowHeight = (dataGridView1.Height - dataGridView1.ColumnHeadersHeight) / 10;
    for (int i = 0; i < 10; ++i)
    {
        dataGridView1.Rows[i].Height = rowHeight;
    }
}

In my example, the Form1_Load event just adds some rows and then calls the DGV's resize to make everything look right initially, but you could handle this different ways. Something like:

private void Form1_Load(object sender, EventArgs e)
{
    for (int i = 0; i < 10; ++i)
    {
        dataGridView1.Rows.Add();
    }
    dataGridView1_Resize(this, EventArgs.Empty);
}
于 2012-09-21T16:49:52.130 回答