DataGridView scrolls to focused row after ResetBindings

Answered
0
0

Hello Support.

I have found that a DataGridView will scroll to it’s focused row (or the top of the list if no row is focused) when the BindingSource that it’s DataSource is set to is changed. For me (well, for my users) this is an issue because if they scroll a couple of pages down in a datagridview and then the data in that datagridview’s BindingList is updated (maybe because another user has added or removed some information) the datagridview will jump back to where the focused row is, losing the position that the user had just scrolled to.

I update a datagridview’s BindingList in the following way (where _jobList is a BindingList set as the DataSource of the DataGridView):

Application.Update(this, () =>
{

_jobList.RaiseListChangedEvents = false;

_jobList.Add(SomeJob);

_jobList.Remove(SomeOtherJob);

_jobList.RaiseListChangedEvents = true;
_jobList.ResetBindings();

}

After this block of code executes the visible rows in the datagridview will be back with the focused row as the first row in the viewport. If the user already had the focused row in their viewport then this is not a problem, they don’t see much happen. But if they have scrolled down a few screenfulls and have not clicked on a row to make it focused then the screen suddenly jumps back to where they were a minute ago. This is a problem !!

Any ideas on how I can make a DataGridView keep the same rows in the viewport after a it’s BindingList is changed ?

Thank you,

Andrew

  • You must to post comments
Great Answer
0
0

Hi Andrew,

Actually there are not property for that job, but I have a simple functionement for do that.

You can open SupportTaskUpdated.zip and add this code:

var number = 5;
InsertSomeItems(number);
list.ScrollRowIntoView(list.FirstDisplayedRowIndex+number,Position.Top);

PS. list is name of DataGridView

Principe is to use scroll to the last first display row plus number of added row.

 

Best.

Kevin(ITG)

  • Andrew Pearce
    Hello Kevin. Thank you, this solution works. For anybody else reading this there was a fix for the ScrollRowIntoView function in version 2.2.36 so you have to be on that version or higher to use this solution.
  • You must to post comments
0
0

Hello Kevin.

Thank you for looking at this for me. I checked over the example you created and it all looked good. I went over my application again and found that the problem was only when rows were inserted into the BindingList at a point above where the user was currently viewing. I didn’t state any of this in my first post, sorry. I have added to the example that you created and attached the amended example to this reply. To see the problem run the attached example and:
– Scroll down to about halfway down the items in the datagridview (I made the program add 30 at the start) but don’t select a row.
– Click the ‘Add data to end’ button. The scroll bar jumps slightly to account for the increase in rows but the datagridview stays with the same rows in the viewport. This looks good.
– Click the ‘Insert data at start’ button. This inserts an object to position 0 in the BindingList. This time instead of the datagridview staying where it is it jumps to the top of the list of items, it behaves differently compared to adding objects to the end of the list.

Is there a way to insert items to the BindingList prior to the user’s currently viewed items and the datagridview viewport to not jump ?
(I found that if you select a row in the datagridview, scroll away from that selected row then click the ‘Insert data at start’ button the datagridview jumps to bring the selected record back into view. If there is no selected row it treats the first row as selected and jumps to the top of the list).

Thanks for your help

Andrew

  • You must to post comments
0
0

Hi Andrew,

I send in attachment the way you can use for keep rows in the viewport after BindingList changed.

When you use BindingList set once time in controller _jobList.RaiseListChangedEvents = true and you are not obliged to _jobList.ResetBindings() after set your modification’s action. BindingList have already an event for change data in DataGridView.

 

Best,

Kevin(ITG)

File supporttasks_datagridview-scrolls-to-focused-row-after-resetbindings,

 

  • You must to post comments
Showing 3 results
Your Answer

Please first to submit.