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