2011-12-26 89 views
14

我在.NET 3.5中遇到了一个错误(我假设)。使用Rows.Add()将行添加到DataGridView时,禁用DGV时,垂直滚动条无法正确更新。因此,在重新启用DGV后,您无法使用滚动条或鼠标滚轮一直滚动到DGV底部(使用箭头键导航仍然有效)。DataGridView垂直滚动条没有正确更新(Forms bug?)

所以我正在寻找解决方法。有没有办法强制滚动条更新其边界,或者你可以手动输入一个新的最大值?我宁愿不必重新填充DGV。

*)实际上,它是禁用的父窗体,但我认为问题在于它传播到DGV控件。

回答

13

我只是有这个问题(同时增加了我行的形式被禁用),并通过设置网格的滚动条属性设置为“无”之前添加行,然后设置回解决这两个“一次我所有的行已添加。

+0

仍然是一个杂牌,但稍微好一点。谢谢。 :) – ReturningTarzan 2012-02-05 00:01:10

+0

超级!保存我))) – Konstantin 2015-12-22 18:01:07

+1

这个工作得很好,但是'SetDataSource'也可以调用'PerformLayout()'! – 2017-01-19 08:53:49

0

其实,我只是找到了一种解决方法,但我不喜欢它。 DGV重新启用后,您可以这样做:

int x = Rows.Add(); 
Rows.RemoveAt(x); 

然后滚动条被更新。但它不是很漂亮,它会引起一个令人讨厌的小闪烁,它可能会引发一些我不得不故意忽略的事件。我会留下这个问题,希望有更好的解决方案。

18

这也解决了这个问题对我来说:

DataGridView.SuspendLayout(); 
DataGridView.ResumeLayout(); 

重新启用之前DGV是它可以被调用。


UPDATE: 这也不会工作:

DataGridView.PerformLayout(); 
+0

我对autosizemode.fill禁用滚动条时遇到问题,在更改内容后执行performlayout也适用于我。谢谢!你救了我的一天! – MazarD 2014-03-17 20:05:21

+0

谢谢!!!其工作 – Raj 2015-05-30 06:00:56

+2

这应该是被接受的答案。也帮助了我。谢谢! – 2015-11-12 12:56:33

0

有人认为,当DataGridView1的宽度和高度分别与宽度和形状的高度,宽度和高度相比如果它们超出了窗体的尺寸,则重置它们,滚动条变得可见。

尝试下面的代码,这将动态DataGridView控件添加到窗体,并创建一个正方形格子的行和列标题名称:

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 
0

我的问题源于在用户线程中调用dgv.Add()。之后将其更改为从UI线程,而不是被调用,滚动条显示和功能正常:

 if (dataGridView1.InvokeRequired) 
     { 
      dataGridView1.Invoke((Action)(() => dataGridView1.Rows.Add(new String[] { abc, efg }))); 
     } 
     else 
     { 
      dataGridView1.Rows.Add(new String[] { calPoint, logUrl }); 
     } 
1

如果没有其他特定解决方案为你工作,我遇到了类似的问题中附带的DataGridView垂直滚动条。但问题就像每当行数超出datagridview的高度时,垂直滚动就会创建一个混乱的UI。各种行相互重叠。

我有一个数据绑定DataGridView。

这些是我尝试过但没有工作的列表。

  1. 将ScrollBars属性设置为None,修改数据源,然后将ScrollBars属性设置为Both。
  2. 以各种组合使用SuspendLayout,ResumeLayout和PerformLayout。
  3. 使用扩展方法为DataGridView设置双缓冲。

最后,设置AutoSizeRowsMo​​deDataGridViewAutoSizeRowsMo​​de.AllCells固定我的问题。

如果你有类似的水平滚动问题,我觉得玩AutoSizeColumnsMode应该解决这个问题。

0

当滑块未正确尺寸和占去了大部分的垂直滚动条我的解决办法是的 -

DGV.height = DGV.Height + 1

DGV.Height = DGV.Height - 1

然后滑块正确尺寸

但我现在用

DGV.PerformLayout

这也解决了问题

-2

我的DataGridView的最后两行总是隐藏在我的WinForms上。我可以使用键盘向下箭头键滚动到他们(但仍然没有看到我实际上在哪一行)。鼠标滚轮和滚动条向下箭头也不会到达他们。只有使用小数据集并使表单最大化,才能看到最后两行。

下面是我如何解决这个问题:我将DataGridView放置在Panel中。 BAM!

它还修复了DataGridView的另一个问题,即当我调整列标题大小时,奇怪的垂直线会出现在DataGridView下的任何UI控件上。这是非常丑陋和不专业的期待。但现在它也是固定的。

0

我在搜索针对我遇到的问题的修复时找到了您的帖子。我在Microsoft Surface(Win10)上遇到的情况是无法使用触摸手势(如轻弹)将DataGridView垂直滚动到长列表的最后一行。通常,最后一行非常难以实现。解决方案很简单,但花了我一段时间才弄清楚。如果有帮助,我会把它留在这里。

// Override WndProc in your custom class inherited from DataGridView 
protected override void WndProc(ref Message m) 
{ 
    switch (m.Msg) 
    { 
    case 0x115:// WM_VSCROLL 
     // The low-order word holds the command 
     uint cmd = ((uint)m.WParam & (uint)0x0000FFFF); 
     switch (cmd) 
     { 
     case 5: // SB_THUMBTRACK 
      if (Rows.Count > 0) 
      { 
      // The high-order word holds the position 
      uint pos = ((uint)m.WParam & (uint)0xFFFF0000) >> 16; 

      // SAVE: This would give us the "true" ratio based on 100% 
      // SAVE: double ratio = (double)pos/(double)(VerticalScrollBar.Maximum - VerticalScrollBar.LargeChange); 
      // SAVE: Debug.WriteLine("Scroll Position: " + pos + "\t" + (ratio * 100.0).ToString("F2") + "%"); 

      // What we want is the ratio to the TOP of the thumb, BECAUSE 
      // THIS GIVES US THE RATIO TO THE FIRST LINE INDEX 
      double firstLineRatio = (double)pos/(double)(VerticalScrollBar.Maximum); 
      // We want to make it so that it shows the full line 
      // even if we just barely meet the ratio 
      double dFirstLine = firstLineRatio * Rows.Count; 
      int iFirstLine = (int)(dFirstLine + 0.9999); 
      // SAVE: Debug.WriteLine("Scroll Position: " + pos + "\t" + (ratio * 100.0).ToString("F2") + "%"); 
      FirstDisplayedScrollingRowIndex = iFirstLine; 
      // We do this INSTEAD OF the default for this message, so RETURN 
      return; 
      } 
      break; 
     } 
     break; 
    default: 
     break; 
    } 
    base.WndProc(ref m); 
} 
1

我的问题是我的垂直滚动条完全消失。我抨击了所有上述建议,并最终发现使包含DataGridView的面板比表单狭窄的面板解决了问题。在我的情况下,16个像素工作较窄。

0

我的解决方案是从它的属性中禁用滚动条。 然后在初始化窗口后从代码行启用它们。 DataGridViewObj.ScrollBars = ScrollBars.Both

0

对我来说,问题是,我把我的数据网格中,因此srolling被搞砸了这是不是在数据产生的时间显示的TabPage

我发现了一个刚刚被自动魔鬼做出正确的更新/使能在每个可见变化:

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; 
    }; 
}