I am trying to stop the DataGridView
from scrolling after ResetBindings
is called.
I call ResetBindings
after I update a value in the list that is bound to it.
After calling ResetBindings
, I am unable to assign a value to FirstDisplayedScrollingRowIndex
.
Please note that no exception is being thrown.
This is the code that I am using:
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;
The above code is in the CellValidating
event for the DataGridView
.
Stepping through the above code:
currentRowIndex
has a value of 12 after the first line is executedResetBindings
is executed which causes FirstDisplayedScrollingRowIndex
to hold the value of 3FirstDisplayedScrollingRowIndex
is not being set to 12 after the last line is executedI am not sure where the value 3 is coming from, but I know it is somehow being applied in the background after ResetBindings
is called.
Can someone help me understand why I am unable to modify the value of FirstDisplayedScrollingRowIndex
after calling ResetBindings
?
All you need to do is change the CurrentCell
in the DataGridView
then you should be able to set the value to FirstDisplayedScrollingRowIndex
.
I know that this is a weird fix but it works for me:
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;
I hope that this helps anyone that got stuck like me on this. Good luck.