English 中文(简体)
Populating a DataGridView on-the-fly (VB.NET)
原标题:

I have a DataGridView which reads data from a custom (CSLA) list object. The dataset might have 100,000 records, and obviously I don t want to show them all at once because, for one, it would take ages to load.

I think it would be nice if I could read the first (for instance) 20 records, and make it so upon scrolling to the end of the list, it reads the next 20 and adds them to the DataGridView.

I have experimented with various ways of doing this, mostly using a custom class inheriting from DataGridView to capture scroll events and raising an event which adds new records. I will include the code below:

Public Class TestDGV
Inherits DataGridView


Public Sub New()

    AddHandler Me.VerticalScrollBar.ValueChanged, AddressOf vsScrollEvent
    AddHandler Me.RowsAdded, AddressOf vsRowsAddedEvent
End Sub

Private Sub vsScrollEvent(ByVal sender As Object, _
                          ByVal e As EventArgs)
    With DirectCast(sender, ScrollBar)

        If .Value >= (.Maximum - .LargeChange) Then
            RaiseEvent ScrollToEnd()
        End If
    End With




End Sub


Private Sub vsRowsAddedEvent(ByVal sender As Object, ByVal e As EventArgs)
    ScrollbarOn()
End Sub

Public Event ScrollToEnd()

Public Sub ScrollbarOff()
    Me.VerticalScrollBar.Enabled = False
End Sub

Public Sub ScrollbarOn()
    Me.VerticalScrollBar.Enabled = True
End Sub

End Class

Though this (sort of) works, it can be buggy. The biggest problem was that if you used the mouse to scroll the DataGrid, it would get stuck in a loop as it process the scrollbar s ValueChanged event after the data was added. That s why I added ScrollbarOff and ScrollbarOn - I call them before and after getting new records, which disables the scrollbar temporarily.

The problem with that is that after the scrollbar is re-enabled, it doesn t keep track of the current mouse state, so if you hold down the Down button with the mouse (or click on part of the scrollbar) it stops scrolling after it has added the new records, and you have to click it again.

Also, it doesn t seem a particularly elegant way of doing things.

Has anyone ever done this before, and how did you achieve it?

Cheers.

问题回答

Perhaps you could reduce your result set and use the << and >> kind of paging controls. Nerd Dinner demo introduced an elegant solution using LINQ:

//
// GET: /Dinners/
//      /Dinners/Page/2

public ActionResult Index(int? page)
{

    const int pageSize = 20;

    var upcomingDinners = dinnerRepository.FindUpcomingDinners();

    var paginatedDinners = upcomingDinners.Skip((page ?? 0) * pageSize)
                                          .Take(pageSize)
                                          .ToList();

    return View(paginatedDinners);
}

What about using the virtualproperty of the DataGridView? See Implementing Virtual Mode with Just-In-Time Data Loading in the Windows Forms DataGridView Control for an example.





相关问题
Is Shared ReadOnly lazyloaded?

I was wondering when I write Shared ReadOnly Variable As DataType = New DataType() Or alternatively Shared ReadOnly Variable As New DataType() Is it lazy loaded or as the instance initializes? ...

Entertaining a baby with VB.NET

I would like to write a little application in VB.NET that will detect a baby s cry. How would I get started with such an application?

Choose Enter Rather than Pressing Ok button

I have many fields in the page and the last field is a dropdown with list of values. When I select an item in a dropdown and press Enter, it doesn t do the "Ok". Instead I have to manually click on Ok ...

ALT Key Shortcuts Hidden

I am using VS2008 and creating forms. By default, the underscore of the character in a textbox when using an ampersand is not shown when I run the application. ex. "&Goto Here" is not ...

Set Select command in code

On button Click I want to Set the Select command of a Gridview. I do this and then databind the grid but it doesn t work. What am i doing wrong? protected void bttnView_Click(object sender, ...

Hover tooltip on specific words in rich text box?

I m trying to create something like a tooltip suddenly hoovering over the mouse pointer when specific words in the richt text box is hovered over. How can this be done?

热门标签