DataGridView vertical scrollbar not updating properly (Forms bug?)

后端 未结 14 1956
伪装坚强ぢ
伪装坚强ぢ 2021-01-03 23:49

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

14条回答
  •  臣服心动
    2021-01-03 23:59

    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
    

提交回复
热议问题