Export (0) Print
Expand All

How to: Change the Appearance of a DataRepeater Control

Visual Studio 2005

You can change the appearance of a DataRepeater control at design time by setting properties or at run time by handling the DrawItem event.

Properties that you set at design time when the item template section of the control is selected will be repeated for each DataRepeaterItem at run time. Appearance-related properties of the DataRepeater control itself will be visible at run time only if a part of the container is left uncovered (for example, if the Padding property is set to a large value).

At run time, appearance-related properties can be set based on conditions. For example, in a scheduling application, you might change the background color of an item to warn users when an item is past due. In the DrawItem event handler, if you set a property in a conditional statement such as If…Then, you must also use an Else clause to specify the appearance when the condition is not met.

To change the appearance at design time

  1. In the Windows Forms Designer, select the item template (upper) region of the DataRepeater control.

  2. In the Properties window, select a property and change the value. Common properties that affect appearance include BackColor, BackgroundImage, BorderStyle, and ForeColor.

To change the appearance at run time

  1. In the Code Editor, in the Event drop-down list, click DrawItem.

  2. In the DrawItem event handler, add code to set the properties:

    e.DataRepeaterItem.BackColor = Color.White 
    For Each c As Control In e.DataRepeaterItem.Controls
        If TypeOf c Is TextBox Then
            c.BackColor = Color.AliceBlue
        Else
            c.BackColor = e.DataRepeaterItem.BackColor
        End If
    Next 
    

Some common customizations for the DataRepeater control include displaying the rows in alternating colors and changing the color of a field based on a condition. The following example shows how to perform these customizations. This example assumes that you have a DataRepeater control that is bound to the Products table in the Northwind database.

Private Sub DataRepeater1_DrawItem(ByVal sender As Object, ByVal e _
 As Microsoft.VisualBasic.PowerPacks.DataRepeaterItemEventArgs) _
 Handles DataRepeater1.DrawItem
    ' Alternate the back color.
    If (e.DataRepeaterItem.ItemIndex Mod 2) <> 0 Then
        ' Apply the secondary back color.
        e.DataRepeaterItem.BackColor = Color.AliceBlue
    Else
        ' Apply the default back color.
        DataRepeater1.ItemTemplate.BackColor = Color.White
    End If
    ' Change the color of out-of-stock items to red.
    If e.DataRepeaterItem.Controls(UnitsInStockTextBox.Name).Text _
 < 1 Then
        e.DataRepeaterItem.Controls(UnitsInStockTextBox.Name). _
 BackColor = Color.Red
    Else
        e.DataRepeaterItem.Controls(UnitsInStockTextBox.Name). _
 BackColor = Color.White
    End If
End Sub

Note that for both of these customizations, you must provide code to set the properties for both sides of the condition. If you do not specify the Else condition, you will see unexpected results at run time.

Community Additions

ADD
Show:
© 2014 Microsoft