Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 无法将值设置为FirstDisplayedScrollingRowIndex_C#_Winforms_Datagridview_Bindingsource - Fatal编程技术网

C# 无法将值设置为FirstDisplayedScrollingRowIndex

C# 无法将值设置为FirstDisplayedScrollingRowIndex,c#,winforms,datagridview,bindingsource,C#,Winforms,Datagridview,Bindingsource,调用ResetBindings后,我试图阻止DataGridView滚动 更新列表中绑定到它的值后,我调用ResetBindings 调用ResetBindings后,我无法为FirstDisplayedScrollingRowIndex赋值 请注意,没有引发任何异常 这是我正在使用的代码: int currentRowIndex = dgvStuff.FirstDisplayedScrollingRowIndex; dgvStuff.CellEnter -= dgvStuff_CellEnt

调用
ResetBindings
后,我试图阻止
DataGridView
滚动

更新列表中绑定到它的值后,我调用
ResetBindings

调用
ResetBindings
后,我无法为
FirstDisplayedScrollingRowIndex
赋值

请注意,没有引发任何异常

这是我正在使用的代码:

int currentRowIndex = dgvStuff.FirstDisplayedScrollingRowIndex;

dgvStuff.CellEnter -= dgvStuff_CellEnter;
dgvStuff.Scroll -= dgvStuff_Scroll;
bindingSource.ResetBindings(false); // FirstDisplayedScrollingRowIndex value gets modified after this call;
dgvStuff.Scroll += dgvStuff_Scroll;
dgvStuff.CellEnter += dgvStuff_CellEnter;

dgvStuff.FirstDisplayedScrollingRowIndex = currentRowIndex; // This value is not being set;
上述代码位于
DataGridView
CellValidating
事件中

逐步执行上述代码:

  • 执行第一行后,
    currentRowIndex
    的值为12
  • ResetBindings
    被执行,这会导致
    FirstDisplayedScrollingRowIndex
    保持3的值
  • 执行最后一行后,
    FirstDisplayedScrollingRowIndex
    未设置为12
我不确定值3是从哪里来的,但我知道在调用
ResetBindings
后,它不知何故正在后台应用

有人能帮我理解为什么调用
ResetBindings
后无法修改
FirstDisplayedScrollingRowIndex
的值吗


您只需更改
DataGridView
中的
CurrentCell
,然后就可以将该值设置为
FirstDisplayedScrollingRowIndex

我知道这是一个奇怪的解决方案,但对我来说很有效:

int rowIndex = dgvStuff.CurrentCell.RowIndex;
int columnIndex = dgvStuff.CurrentCell.ColumnIndex;
int currentRowIndex = dgvStuff.FirstDisplayedScrollingRowIndex;

dgvStuff.CellEnter -= dgvStuff_CellEnter;
dgvStuff.Scroll -= dgvStuff_Scroll;
bindingSource.ResetBindings(false);

if (dgvStuff.RowCount > 0)
{
    dgvStuff.CurrentCell = dgvLEDs[0, 0]; // This line alone fixes the issue;
    dgvStuff.CurrentCell = dgvLEDs[columnIndex, rowIndex]; // I added this line because I needed to specify the current cell;
}

dgvStuff.FirstDisplayedScrollingRowIndex = currentRowIndex; // This value is NOW being set;

dgvStuff.Scroll += dgvStuff_Scroll;
dgvStuff.CellEnter += dgvStuff_CellEnter;

我希望这能帮助像我一样陷入困境的人。祝您好运。

您只需更改
DataGridView
中的
CurrentCell
,然后就可以将值设置为
FirstDisplayedScrollingRowIndex

我知道这是一个奇怪的解决方案,但对我来说很有效:

int rowIndex = dgvStuff.CurrentCell.RowIndex;
int columnIndex = dgvStuff.CurrentCell.ColumnIndex;
int currentRowIndex = dgvStuff.FirstDisplayedScrollingRowIndex;

dgvStuff.CellEnter -= dgvStuff_CellEnter;
dgvStuff.Scroll -= dgvStuff_Scroll;
bindingSource.ResetBindings(false);

if (dgvStuff.RowCount > 0)
{
    dgvStuff.CurrentCell = dgvLEDs[0, 0]; // This line alone fixes the issue;
    dgvStuff.CurrentCell = dgvLEDs[columnIndex, rowIndex]; // I added this line because I needed to specify the current cell;
}

dgvStuff.FirstDisplayedScrollingRowIndex = currentRowIndex; // This value is NOW being set;

dgvStuff.Scroll += dgvStuff_Scroll;
dgvStuff.CellEnter += dgvStuff_CellEnter;
我希望这能帮助像我一样陷入困境的人。祝你好运