Export (0) Print
Expand All

Virtual Mode in the DataRepeater Control

Visual Studio 2005

When you want to display large quantities of tabular data in a DataRepeater control, you can improve performance by setting the VirtualMode property to True and explicitly managing the control's interaction with its data source. The DataRepeater control provides several events that you can handle to interact with your data source and display the data as needed at run time.

The most common scenario for the DataRepeater control is to bind the child controls of the ItemTemplate to a data source at design time and allow the BindingSource to pass data back and forth as needed. When you use virtual mode, the controls are not bound to a data source, and data is passed back and forth to the underlying data source at run time.

When the VirtualMode property is set to True, you create the user interface by adding controls from the Toolbox instead of adding bound controls from the Data Sources window.

Events are raised on a control-by-control basis, and you must add code to handle the display of data. When a new DataRepeaterItem is scrolled into view, the ItemValueNeeded event is raised one time for each control and you must supply the values for each control in the ItemValueNeeded event handler.

If data in one of the controls is changed by the user, the ItemValuePushed event is raised and you must validate the data and save it to your data source.

If the user adds a new item, the NewItemNeeded event is raised. Use this event's handler to create a new record in your data source. To prevent unintended changes, you must also monitor the KeyDown event for each control and call CancelEdit if the user presses the ESC key.

Finally, you must implement event handlers for the ItemsRemoved event, which occurs when an item is deleted, and optionally for the UserDeletingItems and UserDeletedItems events, which occur whenever a user deletes an item by pressing the DELETE key.

Following are the steps that are required to implement virtual mode.

To implement virtual mode

  1. Drag a DataRepeater control from the Toolbox to a form or container control. Set the VirtualMode property to True.

  2. Drag controls from the Toolbox onto the item template region (the upper region) of the DataRepeater control. You will need one control for each field in your data source that you want to display.

  3. Implement a handler for the ItemValueNeeded event to provide values for each control. This event is raised when a new DataRepeaterItem is scrolled into view. The code will resemble the following example, which is for a data source named Employees.

    Private Sub DataRepeater1_ItemValueNeeded(ByVal sender As Object, _
     ByVal e As _
     Microsoft.VisualBasic.PowerPacks.DataRepeaterItemValueEventArgs) _
     Handles DataRepeater1.ItemValueNeeded
        If e.ItemIndex < Employees.Count Then
            Select Case e.Control.Name
                Case _
     Me.DataRepeater1.ItemTemplate.Controls("txtFirstName").Name
                    e.Value = Employees.Item(e.ItemIndex + 1).firstname
                Case _
     Me.DataRepeater1.ItemTemplate.Controls("txtLastName").Name
                    e.Value = Employees.Item(e.ItemIndex + 1).lastname
            End Select
        End If
    End Sub
    
  4. Implement a handler for the ItemValuePushed event to store the data. This event is raised when the user commits changes to a child control of the DataRepeaterItem. The code will resemble the following example, which is for a data source named Employees.

    Private Sub DataRepeater1_ItemValuePushed(ByVal sender As Object, _
     ByVal e As _
     Microsoft.VisualBasic.PowerPacks.DataRepeaterItemValueEventArgs) _
     Handles DataRepeater1.ItemValuePushed
        Select Case e.Control.Name
            Case _
     Me.DataRepeater1.ItemTemplate.Controls("txtFirstName").Name
                Employees.Item(e.ItemIndex + 1).firstname = e.Value
            Case _
     Me.DataRepeater1.ItemTemplate.Controls("txtLastName").Name
                Employees.Item(e.ItemIndex + 1).lastname = e.Value
            Case Else
                p.log.WriteLine("Error during ItemValuePushed _
     unexpected control: " & e.Control.Name)
        End Select
    End Sub
    
  5. Implement a handler for each child control's KeyDown event and monitor the ESC key. Call the CancelEdit method to prevent the ItemValuePushed event from being raised. The code will resemble the following example.

    Private Sub Child_KeyDown(ByVal sender As Object, ByVal e As _
     System.Windows.Forms.KeyEventArgs) Handles txtFirstName.KeyDown, _
     txtLastName.KeyDown
        If e.KeyCode = Keys.Escape Then
            Datarepeater1.CancelEdit()
        End If
    End Sub
    
  6. Implement a handler for the NewItemNeeded event. This event is raised when the user adds a new item to the DataRepeater control. The code will resemble the following example, which is for a data source named Employees.

    Private Sub DataRepeater1_NewItemNeeded(ByVal sender As Object, _
     ByVal e As System.EventArgs) Handles DataRepeater1.NewItemNeeded
        Dim newEmployee As New Employee
        Employees.Add(newEmployee)
        blnNewItemNeedEventFired = True
    End Sub
    
  7. Implement a handler for the ItemsRemoved event. This event occurs when a user deletes an existing item. The code will resemble the following example, which is for a data source named Employees.

    Private Sub DataRepeater1_ItemsRemoved(ByVal sender As Object, _
     ByVal e As _
     Microsoft.VisualBasic.PowerPacks.DataRepeaterAddRemoveItemsEventArgs) _
     Handles DataRepeater1.ItemsRemoved
        Employees.Remove(e.ItemIndex)
    End Sub
    
  8. For control-level validation, optionally implement handlers for the Validating events of the child controls. The code will resemble the following example.

    Private Sub Text_Validating(ByVal sender As Object, ByVal e As _
     System.ComponentModel.CancelEventArgs) Handles _
     txtFirstName.Validating, txtLastName.Validating
        If txtFirstName.Text = "" Then
            MsgBox("Please enter a name.")
            e.Cancel = True
        End If
    End Sub
    

Community Additions

ADD
Show:
© 2014 Microsoft