Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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,
-DA
Posted 30-Jan-13 8:06am
Comments
jibesh at 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.
Donald Allen at 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 at 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
Sergey Alexandrovich Kryukov at 30-Jan-13 14:56pm
   
Why? The problem is very simple — please see my answer.
OP just don't understand how it works.
—SA
jibesh at 30-Jan-13 15:15pm
   
Oh yes.. I remember that property now. thanks for brushing up my mind. :)
Donald Allen at 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 at 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 at 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...
—SA
Donald Allen at 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 at 30-Jan-13 16:37pm
   
Scrolling behavior is universal, no matter if it's binding or not.
—SA
Donald Allen at 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
Donald Allen at 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

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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:
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.firstdisplayedscrollingrowindex.aspx[^].
 
See also:
http://stackoverflow.com/questions/8437916/how-do-i-make-the-datagridview-show-the-selected-row-winforms[^],
http://social.msdn.microsoft.com/Forums/en-US/winformsdatacontrols/thread/7db968a9-38be-45fc-9dbf-e283f39d9e8c/[^].
 
Anything else?
 
—SA
  Permalink  

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



Advertise | Privacy | Mobile
Web04 | 2.8.141022.2 | Last Updated 30 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100