Click here to Skip to main content
14,698,679 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a winForm with a dataGridView control that adds and removes rows every few seconds. When the number of rows exceeds the length of the dataGridView the scroll keeps moving and resizing itself! How do I stop it from moving and resizing as rows are being added and removed?

Thanks in advance,
Jibesh 30-Jan-13 14:13pm
one simple method will be keeping the first row as selected. whenever you insert a new row to the datagridview set the first row as selected so that you can avoid the auto scroll to the previous selected row.
d.allen101 30-Jan-13 14:17pm
i'm not inserting i'm adding and it's more than just keeping the first row visible I want to bar to stop resizing and for the bar to move only if the user moves it
Jibesh 30-Jan-13 14:25pm
You cannot stop the scroll bar resize and it does automatically based on the number of rows in the datagridview. there are other ways to control this but it takes lot of code - hide the scroll bar and implement the pagination like view for your grid and limit the no of rows that can display in your grid for the current height.

No matter whether you insert/add - have you tried to set the first row as selected and check the behavior it may satisfies the scrollbar jumping around issue
d.allen101 30-Jan-13 14:34pm
yeah, sorry for jumping the gun and not even trying to implement your advice. i'll give it try and see how it works? why am i not able to stop the scrollbar from resizing? if that's the case that's so goofy of MSFT to even have the bar resize. it's makes no sense what so ever!
Jibesh 30-Jan-13 14:50pm
the thing is, if your row increases beyond the display area of the grid how will you scroll to the last record. try to use your grid with ScrollBar property None and you can find the difficulty in using it, atleast your user will complain that he cannot scroll and cant see the the remaining records.
Sergey Alexandrovich Kryukov 30-Jan-13 14:56pm
Why? The problem is very simple — please see my answer.
OP just don't understand how it works.
d.allen101 30-Jan-13 14:56pm
i think may have something to do with the dataBinding. I never see the scrollbar resize if rows are added and removed to an unbound dataGridView
Sergey Alexandrovich Kryukov 30-Jan-13 14:58pm
How come you cannot understand the obvious purpose of this behavior, which is nearly perfect?
And you never explained why do you think it's bad. Why it's "goofy"? Come on...
Jibesh 30-Jan-13 15:15pm
Oh yes.. I remember that property now. thanks for brushing up my mind. :)
Sergey Alexandrovich Kryukov 30-Jan-13 16:37pm
Scrolling behavior is universal, no matter if it's binding or not.
d.allen101 30-Jan-13 16:40pm
I NEVER see the size of the bar change on any of my unbound dataGridViews and I NEVER seen it (bar/thumb) move if I add or delete a row from an unbound dataGridView
d.allen101 30-Jan-13 16:42pm
I'll accept your solution because you pointed to the MSFT links that "somewhat" address the issue I'm having. I think I need to get a much better understanding of how the scrollBar works.

1 solution

You should never stop it! This is a perfectly well defined behavior. The thumb of a scroll bar gets smaller size when you add a row, to reflect its size relative to the summary height of all rows. Is it clear?

What you might really need is the ability to scroll vertically to some row you want to show. Please see:[^].

See also:[^],[^].

Anything else?


This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900