【发布时间】:2010-05-12 22:29:58
【问题描述】:
我正在处理一个用 VS 2005 编写的项目(修复错误)。表单上有一个 DataGridView 控件。首次加载时,控件的数据网格会手动或以代码的形式填充来自集合的数据行。实际上,有方法 PopulateDataGrid() 可以完成这项工作。
表单上还有另一个控件。当控制改变时,数据网格将首先被清除,然后通过 PopulateDataGrid() 再次重新填充行。问题是刷新网格时,垂直滚动条没有正确重置。我认为应该是。由于滚动条未重置,当我尝试单击网格并向下移动时,出现异常:{“‘222’的值对‘值’无效。‘值’应介于‘最小值’和‘最大值’之间'.\r\n参数名称: 值"}:
at System.Windows.Forms.ScrollBar.set_Value(Int32 value)
at System.Windows.Forms.DataGridView.ScrollRows(Int32 rowCount, Int32 deltaY, ScrollEventType scrollEventType)
at System.Windows.Forms.DataGridView.ScrollRowsByCount(Int32 rows, ScrollEventType scrollEventType)
at System.Windows.Forms.DataGridView.ScrollRowIntoView(Int32 columnIndex, Int32 rowIndex, Boolean committed, Boolean forCurrentCellChange)
at System.Windows.Forms.DataGridView.ScrollIntoView(Int32 columnIndex, Int32 rowIndex, Boolean forCurrentCellChange)
at System.Windows.Forms.DataGridView.ProcessDownKeyInternal(Keys keyData, Boolean& moved)
at System.Windows.Forms.DataGridView.ProcessDataGridViewKey(KeyEventArgs e)
at System.Windows.Forms.DataGridView.OnKeyDown(KeyEventArgs e)
at System.Windows.Forms.Control.ProcessKeyEventArgs(Message& m)
...
网格控制的所有设置都是默认值。例如,ScrollBars 是 Both。以下是设置行自动大小属性的唯一相关位置:
poDataGridView.AutoSizeRowsMode =
DataGridViewAutoSizeRowsMode.DisplayedCellsExceptHeaders;
我不确定是否需要在设计器中设置任何属性?
【问题讨论】:
-
如果您尝试将 CurrentCell 设置为 null 会发生什么? (stackoverflow.com/questions/341018/…)
-
问题是当我下次尝试刷新数据时,行数可能会更多。我不能简单地将所有单元格设置为 null 而不是 clear()
-
我的意思不是让您通过将它们设置为 null 来清除所有单元格,我的意思是在您清除行之后将 YourDataGridViewName.CurrentCell 设置为 null 。那应该重置当前单元格位置,这才是问题的症结所在。
-
好的。我明白你的意思了。我明天试一试,然后告诉你。谢谢!
-
我刚刚通过远程访问我的 PC 进行了尝试。我试图在 gd.Rows.Clear() 之后设置 CurrentCell = null 并且在重新填充所有行之后。问题又来了。抱歉,它不起作用。重置 ScrollBar 工作正常。
标签: visual-studio-2005 datagridview