다음을 통해 공유


방법: DataRepeater 컨트롤의 모양 변경(Visual Studio)

업데이트: 2008년 7월

디자인 타임에 속성을 설정하거나 런타임에 DrawItem 이벤트를 처리하여 DataRepeater 컨트롤의 모양을 변경할 수 있습니다.

디자인 타임에 컨트롤의 항목 템플릿 섹션을 선택할 때 설정한 속성이 런타임에 각 DataRepeaterItem에 대해 반복됩니다. DataRepeater 컨트롤 자체의 모양 관련 속성은 Padding 속성이 큰 값으로 설정된 경우와 같이 컨테이너의 일부가 덮여 있지 않은 경우만 런타임에 표시됩니다.

런타임에 조건에 따라 모양 관련 속성을 설정할 수 있습니다. 예를 들어 일정 응용 프로그램에서 항목이 기한을 넘기면 항목의 배경색을 변경하여 사용자에게 경고할 수 있습니다. DrawItem 이벤트 처리기에서 If...Then와 같은 조건문에 속성을 설정하는 경우 Else 절을 사용하여 조건에 맞지 않을 때의 모양도 지정해야 합니다.

디자인 타임에 모양을 변경하려면

  1. Windows Forms 디자이너에서 DataRepeater 컨트롤의 항목 템플릿(위쪽) 영역을 선택합니다.

  2. 속성 창에서 속성을 선택하고 값을 변경합니다. 모양에 영향을 주는 공용 속성에는 BackColor, BackgroundImage, BorderStyleForeColor가 있습니다.

런타임에 모양을 변경하려면

  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.
        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
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.
        dataRepeater1.ItemTemplate.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 컨트롤 소개(Visual Studio)

참조

DataRepeater

DrawItem

변경 기록

날짜

변경 내용

원인

2008년 7월

추가된 항목입니다.

SP1 기능 변경