I\'ve encountered a bug (I assume) in .NET 3.5. When adding rows to a DataGridView using Rows.Add(), while the DGV is disabled, the vertical scrollbar doesn\'t update proper
My problem was that my vertical scrollbar disappeared completely. I flailed with all of the above suggestions and finally discovered that making the panel containing the DataGridView narrower than the form solved the problem. In my case, 16 pixels narrower worked.
If none of the other given solution worked for you, I came across a similar issue with vertical scrollbar in DataGridView. But the issue is like whenever the number of rows extend beyond the height of the datagridview, vertical scrolling created a messed up UI. Kind of rows overlapping each other.
I had a databound DataGridView.
These are the list of things I tried but didn't work.
Finally, Setting AutoSizeRowsMode to DataGridViewAutoSizeRowsMode.AllCells fixed the issue for me.
If you have similar issue with horizontal scrolling, I think playing with AutoSizeColumnsMode should fix the issue.
For me the problem was that I put my datagrid in a TabPage
which was not displayed during data generation time so the srolling was messed up.
I found a was to make a correct update just by auto diable/enable at each visible change :
public MyForm()
{
InitializeComponent();
// Automatic scroll to bottom (it might not work if you try to do it without this event)
datagrid.RowsAdded += (sender, e) =>
{
datagrid.FirstDisplayedScrollingRowIndex = datagrid.RowCount - 1;
};
// WinForms bug fix: scrollbar update
datagrid.VisibleChanged += (sender, e) =>
{
Enabled = false;
Enabled = true;
};
}
The last two rows of my DataGridView were always hidden on my WinForms. I could scroll to them using the keyboard down arrow key (but still not see which row I was actually on). The mouse wheel and scrollbar down arrow would not get to them either. Only with a small data set and maximizing the form could I see the last two rows.
Here is how I fixed the problem: I placed the DataGridView in a Panel. BAM!
It also fixed another problem with the DataGridView, that when I resized a column headers weird vertical lines would appear on any UI control below the DataGridView. It was very ugly and unprofessional looking. But now it is fixed too.
It was observed that, when the DataGridView1's width and height were compared with the width and height of the form, and the width and height were reset if they exceeded the form's dimensions, the scroll bars became visible.
Try the following code, which will dynamically add a DataGridView control to a Form and create a square grid with row and column header names:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
'Following code adds a Datagridview control to a Form dynamically
'Step 1. Add a textbox to a Form, and input the number of columns (ncol). (Note: in this example, ncol=nrow).
'Step 2. Set the Form's Windowstate property to Maximized
For Each cont As Control In Me.Controls 'remove DataGridView if it already exists on the Form
If TypeOf (cont) Is DataGridView Then
Me.Controls.Remove(cont)
End If
Next
Dim DataGridView1 As New DataGridView 'create new data grid view dynamically during run-time
Me.Controls.Add(DataGridView1) 'add the data grid view to the Form
Me.Refresh()
Dim i, nrow, ncol As Integer ' ncol=nrow -->this is a square grid
ncol = TextBox1.Text
nrow = ncol 'Note: add a second textbox to the form and input nrow if you don't want a square grid
DataGridView1.Visible = True
DataGridView1.Top = 100
DataGridView1.Left = 100
DataGridView1.Rows.Clear()
Do While DataGridView1.Columns.Count > 0
DataGridView1.Columns.RemoveAt(DataGridView1.Columns.Count - 1)
Loop
For i = 1 To ncol
DataGridView1.Columns.Add(i, "V" & i)
Next
DataGridView1.Width = ncol * 115
DataGridView1.Height = nrow * 22 + 45
If DataGridView1.Width > Me.Width - DataGridView1.Left Then DataGridView1.Width = Me.Width - DataGridView1.Left - 20
If DataGridView1.Height > Me.Height - DataGridView1.Top Then DataGridView1.Height = Me.Height - DataGridView1.Top - 50
DataGridView1.ScrollBars = ScrollBars.None
For i = 1 To nrow
DataGridView1.Rows.Add()
DataGridView1.Rows.Item(i - 1).HeaderCell.Value = "V" & i
Next
DataGridView1.AutoResizeRowHeadersWidth(DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders)
Dim dgvColumnHeaderStyle As New DataGridViewCellStyle()
dgvColumnHeaderStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
DataGridView1.ColumnHeadersDefaultCellStyle = dgvColumnHeaderStyle
DataGridView1.AllowUserToAddRows = False
DataGridView1.ScrollBars = ScrollBars.Both
Me.WindowState = FormWindowState.Maximized
End Sub
My solution was to disable scrollbars from it's properties. Then enable them from code line after initializing the window. DataGridViewObj.ScrollBars = ScrollBars.Both