Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm Controls / June 2008

Tip: Looking for answers? Try searching our database.

How to disable (Auto)Scroll for a DataGridView

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Paul Schwann - 25 Jun 2008 13:45 GMT
Hi Group,

I am using C# with .NET 2.0 and Visual Studio 2005. I have a GUI with
a DataGridView (DGV) and a BindingSource for the data. The data are
added programmatically, up to 5 rows per second.

My question are:

How do I disable the automatic scrolling of the DGV? It seems that
each time I add a new data record, DGV scrolls to the position of that
new record.

How do I prevent the DGV from keeping the position of a selected row
(each time I add a new record, the DGV scrolls back to the selection).

What I want at the end is a DGV which shows a scroll bar but does not
do ANY automatic maintenance of it. The user alone shall decide what
portion of the DGV is shown even if the data are added, removed
selected or what else...

Thanks for any ideas!

Regards,
Paul
Patrick B. - 25 Jun 2008 19:58 GMT
Paul,

That's kind of interesting. So you're adding data to the DataGridView in
real-time while the user already has the form open? You do that in a
separate thread?

I'm not sure this matters, but how are you adding records to the DGV?
Are you just adding the records to the DataTable connected to the DGV by
the BindingSource?

Also, why does it take a full second to load 5 rows? That seems really slow.

Is your DGV sorted? Or do new records always pop into the DGV at the bottom?

I'm curious to hear more about your application. I think what you want
to accomplish with the DGV is probably possible, but I also have a
feeling that you might be able to solve your problem by employing a
different strategy that doesn't involve tweaking the DGV.

Patrick

> Hi Group,
>
[quoted text clipped - 20 lines]
> Regards,
> Paul
Paul Schwann - 26 Jun 2008 07:15 GMT
Hi Patrick,

> That's kind of interesting. So you're adding data to the DataGridView in
> real-time while the user already has the form open? You do that in a
> separate thread?

Yes, the data are produced in a separate thread.

> I'm not sure this matters, but how are you adding records to the DGV?
> Are you just adding the records to the DataTable connected to the DGV by
> the BindingSource?

The data are added to the binding source, it looks like:

// get the binding source
BindingSource bindingSource = (BindingSource)this.DataSource;

// add new message at the beginning
bindingSource.Insert(0, ${record});

A new message is always added to the top of the table. Thus, older
records are moving down.

> Also, why does it take a full second to load 5 rows? That seems really slow.

This is just because the external data source delivers not faster than
5/sec max.

> Is your DGV sorted? Or do new records always pop into the DGV at the bottom?

It's not sorted. New data are placed on the top.

> I'm curious to hear more about your application. I think what you want
> to accomplish with the DGV is probably possible, but I also have a
> feeling that you might be able to solve your problem by employing a
> different strategy that doesn't involve tweaking the DGV.

I think the problem I have has nothing to do with the data themselves.
Its more how the DGV works and how it handles scrolling and
selection.

Regards,
Paul
Patrick B. - 26 Jun 2008 19:29 GMT
Paul,

I've been able to add records to the bottom of a DGV, and it doesn't
scroll. From the user's POV, they have no idea new records were added
unless they purposefully scroll to the bottom. But adding new records to
the top of a DGV is more complicated. The currently visible records MUST
move down, which disturbs the user's viewing experience.

Maybe rather than adding the new records to the DGV as they "arrive",
you could store them, and then have another thread that activates every
5 seconds, takes all the new records waiting in storage and appends them
to the DGV. So you add your new records in bursts. Use
FirstDisplayedScrollingRowIndex to find the index of the row that is
currently on the top of the visible area of the DGV, then immediately
after you add the new records, set FirstDisplayedScrollingRowIndex to
that index plus the number of newly added rows. It should happen fast
enough that it won't impact the user's experience.

Here is the general idea:

void ThreadThatActivatesEveryFiveSeconds()
{
    int currentIndex = DGV.FirstDisplayedScrollingRowIndex;
    int numberOfNewlyAddedRecords = AddPendingRecordsToTopOfDGV();
    DGV.FirstDisplayedScrollingRowIndex = currentIndex +
numberOfNewlyAddedRecords;
}

Thanks!

Patrick
Paul Schwann - 27 Jun 2008 17:47 GMT
Hi Patrick

> I've been able to add records to the bottom of a DGV, and it doesn't
> scroll.

Me too. The problem is not adding the records but the fact that the
DGV scrolls to the last manually selected record each time a new one
is added. To reproduce the behavior that I encounter and do not like
try this:

- Run your DGV app.
- Select one (or more) record(s) with your mouse.
- try to scroll to another position inside the DGV.
-> "autoscroll" will scroll back to your selected record right after a
new record was added.
--> I hate this behavior and want to turn it off. I don't know how.
--> Disabling selection by the user is not option because I need
selected records for further processing.

Regards,
Paul

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.