方法 : DataRepeater コントロールの外観を変更する (Visual Studio)

DataRepeater コントロールの外観は、デザイン時にプロパティを設定するか、実行時に DrawItem イベントを処理することで変更できます。

デザイン時にコントロールの項目テンプレート セクションを選択した状態で設定するプロパティは、実行時に DataRepeaterItem ごとに繰り返されます。DataRepeater コントロール自体の外観に関連するプロパティは、実行時にコンテナーの一部が隠されていない状態の場合 (Padding プロパティが大きい値に設定されている場合など) に限り、表示されます。

外観に関連するプロパティは、実行時に、条件に基づいて設定できます。たとえば、スケジュール管理用のアプリケーションでは、項目の背景色を変更して、期限切れの項目を警告できます。DrawItem イベント ハンドラー内で、If…Then などの条件付きステートメントでプロパティを設定する場合は、Else 句も使用して、条件が満たされない場合の外観を指定する必要があります。

デザイン時に外観を変更するには

  1. Windows フォーム デザイナーで、DataRepeater コントロールの項目テンプレート (上部) 領域を選択します。

  2. [プロパティ] ウィンドウで、プロパティを選択して値を変更します。外観に関連する一般的なプロパティとしては、BackColorBackgroundImageBorderStyleForeColor などがあります。

実行時に外観を変更するには

  1. コード エディターで、[イベント] ボックスの一覧の [DrawItem] をクリックします。

  2. DrawItem イベント ハンドラーに、プロパティを設定するコードを追加します。

    ' Set the default BackColor.
    e.DataRepeaterItem.BackColor = Color.White
    ' Loop through the controls on the DataRepeaterItem.
    For Each c As Control In e.DataRepeaterItem.Controls
        ' Check the type of each control.
        If TypeOf c Is TextBox Then
            ' If a TextBox, change the BackColor.
            c.BackColor = Color.AliceBlue
        Else
            ' Otherwise use the default BackColor.
            c.BackColor = e.DataRepeaterItem.BackColor
        End If
    Next
    
    // Set the default BackColor.
    e.DataRepeaterItem.BackColor = Color.White;
    // Loop through the controls on the DataRepeaterItem.
    foreach (Control c in e.DataRepeaterItem.Controls)
    {
        // Check the type of each control.
        if (c is TextBox)
        // If a TextBox, change the BackColor.
        {
            c.BackColor = Color.AliceBlue;
        }
        else
        {
            // Otherwise use the default BackColor.
            c.BackColor = e.DataRepeaterItem.BackColor;
        }
    }
    

使用例

DataRepeater コントロールの一般的なカスタマイズの例として、行の色を交互にする、条件に基づいてフィールドの色を変更する、などが挙げられます。このようなカスタマイズを行う方法を次の例に示します。この例では、Northwind データベースの Products テーブルにバインドされた DataRepeater コントロールがあることを前提としています。

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.
        e.DataRepeaterItem.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
private void dataRepeater1_DrawItem(object sender, 
    Microsoft.VisualBasic.PowerPacks.DataRepeaterItemEventArgs e)
{
    // Alternate the back color.
    if ((e.DataRepeaterItem.ItemIndex % 2) != 0)
    // Apply the secondary back color.
    {
        e.DataRepeaterItem.BackColor = Color.AliceBlue;
    }
    else
    {
        // Apply the default back color.
        e.DataRepeaterItem.BackColor = Color.White;
    }
    // Change the color of out-of-stock items to red.
    if (e.DataRepeaterItem.Controls["unitsInStockTextBox"].Text == "0")
    {
        e.DataRepeaterItem.Controls["unitsInStockTextBox"].BackColor = Color.Red;
    }
    else
    {
        e.DataRepeaterItem.Controls["unitsInStockTextBox"].BackColor = Color.White;
    }
}

どちらのカスタマイズを行うときも、条件の一致不一致両方の場合について、プロパティを設定するコードを記述する必要があります。Else 条件を指定しないと、実行時に予期しない結果が発生します。

参照

処理手順

DataRepeater コントロールのトラブルシューティング (Visual Studio)

方法 : DataRepeater コントロールにバインドされたデータを表示する (Visual Studio)

方法 : DataRepeater コントロールに非バインド データを表示する (Visual Studio)

方法 : DataRepeater コントロールに項目ヘッダーを表示する (Visual Studio)

関連項目

DataRepeater

DrawItem

概念

DataRepeater コントロールの概要 (Visual Studio)