visual-studio-2005 - DataGridView:滚动条不刷新

标签 visual-studio-2005 datagridview

我正在处理(修复错误)一个用 VS 2005 编写的项目。窗体上有一个 DataGridView 控件。首次加载时,控件的数据网格会手动或通过代码填充集合中的数据行。实际上,有方法 PopulateDataGrid() 可以完成这项工作。

窗体上还有另一个控件。更改控件时,将首先清除数据网格,然后通过 PopulateDataGrid() 重新填充行。问题是当刷新网格时,垂直滚动条没有正确重置。我认为应该如此。由于滚动条未重置,当我尝试单击网格并向下移动时,出现异常:{"Value of '222' is not valid for 'Value'. 'Value' should be between 'minimum' and 'maximum '.\r\n参数名称:Value"}:

 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;

我不确定是否需要在设计器中设置任何属性?

最佳答案

我想我已经解决了问题。我必须在刷新之前将控件的滚动条设置为无,并在我的刷新方法调用中重置为两者:

 private void PopulateDataGrid() {
    dataGrid.Rows.Clear();
    dataGrid.ScrollBars = ScrollBars.None;
    // continue to get new data and populate cells....
    dataGrid.ScrollBars = ScrollBars.Both;
 }

关于visual-studio-2005 - DataGridView:滚动条不刷新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2823220/

相关文章:

c# - C# 和 Visual Studio 2005 程序集之间的循环引用

c++ - 编译随机失败: "cannot open program database"

c# - 如何声明多个计时器并在时间到期时停止计时器

.net - 连接 .NET DataSet 中的表以在 DataGridView 中显示

c++ - 如何在 std::map 中使用结构作为键

c - 在 MSVC 2005 中执行 C 代码时出现奇怪的错误

visual-studio-2005 - Visual Studio : "Apply Code Changes" is disabled

c++ - 访问 dataGridView 列中的组合框?

c# - SQLite + EntityFramework 6 设置

c# - 在更新到数据库之前编辑 DataTable 中的行