.netvb.netwinformsdatagridviewscrollbars

Why does my DataGridView resist displaying the bottom data and how do I fix it?


I have a form with a DataGridView. I format it like this:

dgvECN.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.None
dgvECN.Columns(0).Width = 50
...
dgvECN.Columns(15).Width = 100
dgvECN.DefaultCellStyle.WrapMode = DataGridViewTriState.True

The grid always has more data than will appear on one screen, so I rely on scrollbars -- primarily the vertical. If I grab the scrollbar with the mouse and drag it as far down as it will go, it stops maybe ~97% of the way down the bar, as if I'd reached the bottom and there are more rows of data that I haven' not yet scrolled to. I can release the scrollbar and grab it again and drag it down and it will go down further, but not all the way. On the fourth try (with the current data), it reaches the bottom.

I can hold down the down-arrow at the bottom of the scroll-bar and that works. I can cursor/arrow down or page-down all the way to the bottom. When I do any of those things, the scroll-handle jitters down and then up a little like it's recalculating or something.

I have tried having the form load with the bottom of the data showing by using:

dgvECN.FirstDisplayedScrollingRowIndex = dgvECN.RowCount - 1

But that scrolls the view to around the region of data where the grid seems to initially think the bottom is, rather than the actual bottom.

I think this is my first time working with wrapping text inside the grid's cells, so I suspect that as a cause.

But I've been reading and reading and while I've found plenty of exotic bugs and errors that sound related at first, they all end up not being my issue and I haven't found anything that fixes it.

Feel free to ask in the comment for specific info about my project, I'm not sure what you might need to help diagnose this. And thanks for your time!

ETA: .NET 4.0 and VS Express 2013, in case it matters.

ETA2: I've attempted to set the on-load display so that it's showing the bottom of the data rather than that top. I'm doing this with:

dgvECN.CurrentCell = dgvECN.Item(0, dgvECN.Rows.Count - 1)

But that doesn't actually take it to the bottom of the data. I imagine this is a manifestation of the same error.

ETA3: This is what it looks like when it loads with the current cell set to the bottom row: bugged DataGridView

ETA4: the DataGridView Designer code:

    '
    'dgvECN
    '
    Me.dgvECN.AllowUserToAddRows = False
    Me.dgvECN.AllowUserToDeleteRows = False
    Me.dgvECN.AllowUserToOrderColumns = True
    Me.dgvECN.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
        Or System.Windows.Forms.AnchorStyles.Left) _
        Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
    Me.dgvECN.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.Fill
    Me.dgvECN.AutoSizeRowsMode = System.Windows.Forms.DataGridViewAutoSizeRowsMode.DisplayedCellsExceptHeaders
    Me.dgvECN.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize
    Me.dgvECN.Columns.AddRange(New System.Windows.Forms.DataGridViewColumn() {Me.Check})
    Me.dgvECN.EditMode = System.Windows.Forms.DataGridViewEditMode.EditProgrammatically
    Me.dgvECN.Location = New System.Drawing.Point(3, 3)
    Me.dgvECN.Name = "dgvECN"
    Me.dgvECN.Size = New System.Drawing.Size(1102, 670)
    Me.dgvECN.TabIndex = 5

Solution

  • Here is a test program for this problem (written in C#, but should be applicable to VB). It assumes you have one DataGridView named dataGridView1.

    The AutoSizeRowsMode property should be set to DataGridViewAutoSizeRowsMode.AllCells to avoid the problem you are facing.

            DataTable data = new DataTable("Example");
            data.Columns.Add("1");
            data.Columns.Add("2");
            data.Columns.Add("3");
            data.Columns.Add("4");
    
            for (int i = 0; i < 2000; i++)
            {
                data.Rows.Add(new []
                    {
                        "1",
                        "2",
                        "LONG STRING THAT SHOULD WRAP TO MULTIPLE LINES",
                        "4"
                    });
            }
            data.Rows.Add(new[]
                    {
                        "1",
                        "2",
                        "LONG STRING THAT SHOULD WRAP TO MULTIPLE LINES",
                        "Last Row!!!"
                    });
    
            dataGridView1.AllowUserToAddRows = false;
            dataGridView1.AllowUserToDeleteRows = false;
            dataGridView1.AllowUserToOrderColumns = true;
            dataGridView1.AutoSizeColumnsMode =DataGridViewAutoSizeColumnsMode.Fill; 
            dataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.DisplayedCellsExceptHeaders; // Appears that this line should be `AllCells` to avoid the problem you are facing
            dataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            dataGridView1.DefaultCellStyle.WrapMode = DataGridViewTriState.True;
            dataGridView1.EditMode = DataGridViewEditMode.EditProgrammatically;
            dataGridView1.DataSource = data;