DataGridViewCellFormattingEventHandler 대리자

정의

CellFormattingDataGridView 이벤트를 처리할 메서드를 나타냅니다.

public delegate void DataGridViewCellFormattingEventHandler(System::Object ^ sender, DataGridViewCellFormattingEventArgs ^ e);
public delegate void DataGridViewCellFormattingEventHandler(object sender, DataGridViewCellFormattingEventArgs e);
public delegate void DataGridViewCellFormattingEventHandler(object? sender, DataGridViewCellFormattingEventArgs e);
type DataGridViewCellFormattingEventHandler = delegate of obj * DataGridViewCellFormattingEventArgs -> unit
Public Delegate Sub DataGridViewCellFormattingEventHandler(sender As Object, e As DataGridViewCellFormattingEventArgs)

매개 변수

sender
Object

이벤트 소스입니다.

e
DataGridViewCellFormattingEventArgs

이벤트 데이터를 포함하는 DataGridViewCellFormattingEventArgs입니다.

예제

다음 코드 예제에서는 처리 하는 방법에 설명 합니다 CellFormatting 이벤트입니다.

void dataGridView1_CellFormatting( Object^ /*sender*/, DataGridViewCellFormattingEventArgs^ e )
{
   // If the column is the Artist column, check the
   // value.
   if ( this->dataGridView1->Columns[ e->ColumnIndex ]->Name->Equals( "Artist" ) )
   {
      if ( e->Value != nullptr )
      {
         // Check for the string "pink" in the cell.
         String^ stringValue = dynamic_cast<String^>(e->Value);
         stringValue = stringValue->ToLower();
         if ( (stringValue->IndexOf( "pink" ) > -1) )
         {
            DataGridViewCellStyle^ pinkStyle = gcnew DataGridViewCellStyle;

            //Change the style of the cell.
            pinkStyle->BackColor = Color::Pink;
            pinkStyle->ForeColor = Color::Black;
            pinkStyle->Font = gcnew System::Drawing::Font( "Times New Roman",8,FontStyle::Bold );
            e->CellStyle = pinkStyle;
         }
         
      }
   }
   else
   if ( this->dataGridView1->Columns[ e->ColumnIndex ]->Name->Equals( "Release Date" ) )
   {
      ShortFormDateFormat( e );
   }
}


//Even though the date internaly stores the year as YYYY, using formatting, the
//UI can have the format in YY.  
void ShortFormDateFormat( DataGridViewCellFormattingEventArgs^ formatting )
{
   if ( formatting->Value != nullptr )
   {
      try
      {
         System::Text::StringBuilder^ dateString = gcnew System::Text::StringBuilder;
         DateTime theDate = DateTime::Parse( formatting->Value->ToString() );
         dateString->Append( theDate.Month );
         dateString->Append( "/" );
         dateString->Append( theDate.Day );
         dateString->Append( "/" );
         dateString->Append( theDate.Year.ToString()->Substring( 2 ) );
         formatting->Value = dateString->ToString();
         formatting->FormattingApplied = true;
      }
      catch ( Exception^ /*notInDateFormat*/ ) 
      {
         // Set to false in case there are other handlers interested trying to
         // format this DataGridViewCellFormattingEventArgs instance.
         formatting->FormattingApplied = false;
      }

   }
}
private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
    // If the column is the Artist column, check the
    // value.
    if (this.dataGridView1.Columns[e.ColumnIndex].Name == "Artist")
    {
        if (e.Value != null)
        {
            // Check for the string "pink" in the cell.
            string stringValue = (string)e.Value;
            stringValue = stringValue.ToLower();
            if ((stringValue.IndexOf("pink") > -1))
            {
                e.CellStyle.BackColor = Color.Pink;
            }
        }
    }
    else if (this.dataGridView1.Columns[e.ColumnIndex].Name == "Release Date")
    {
        ShortFormDateFormat(e);
    }
}

//Even though the date internaly stores the year as YYYY, using formatting, the
//UI can have the format in YY.  
private static void ShortFormDateFormat(DataGridViewCellFormattingEventArgs formatting)
{
    if (formatting.Value != null)
    {
        try
        {
            System.Text.StringBuilder dateString = new System.Text.StringBuilder();
            DateTime theDate = DateTime.Parse(formatting.Value.ToString());

            dateString.Append(theDate.Month);
            dateString.Append("/");
            dateString.Append(theDate.Day);
            dateString.Append("/");
            dateString.Append(theDate.Year.ToString().Substring(2));
            formatting.Value = dateString.ToString();
            formatting.FormattingApplied = true;
        }
        catch (FormatException)
        {
            // Set to false in case there are other handlers interested trying to
            // format this DataGridViewCellFormattingEventArgs instance.
            formatting.FormattingApplied = false;
        }
    }
}
Private Sub dataGridView1_CellFormatting(ByVal sender As Object, _
    ByVal e As DataGridViewCellFormattingEventArgs) _
    Handles dataGridView1.CellFormatting
    ' If the column is the Artist column, check the
    ' value.
    If Me.dataGridView1.Columns(e.ColumnIndex).Name _
        = "Artist" Then
        If e.Value IsNot Nothing Then

            ' Check for the string "pink" in the cell.
            Dim stringValue As String = _
            CType(e.Value, String)
            stringValue = stringValue.ToLower()
            If ((stringValue.IndexOf("pink") > -1)) Then
                e.CellStyle.BackColor = Color.Pink
            End If

        End If
    ElseIf Me.dataGridView1.Columns(e.ColumnIndex).Name _
        = "Release Date" Then
        ShortFormDateFormat(e)
    End If
End Sub

'Even though the date internaly stores the year as YYYY, using formatting, the
'UI can have the format in YY.  
Private Shared Sub ShortFormDateFormat(ByVal formatting As DataGridViewCellFormattingEventArgs)
    If formatting.Value IsNot Nothing Then
        Try
            Dim dateString As System.Text.StringBuilder = New System.Text.StringBuilder()
            Dim theDate As Date = DateTime.Parse(formatting.Value.ToString())

            dateString.Append(theDate.Month)
            dateString.Append("/")
            dateString.Append(theDate.Day)
            dateString.Append("/")
            dateString.Append(theDate.Year.ToString().Substring(2))
            formatting.Value = dateString.ToString()
            formatting.FormattingApplied = True
        Catch notInDateFormat As FormatException
            ' Set to false in case there are other handlers interested trying to
            ' format this DataGridViewCellFormattingEventArgs instance.
            formatting.FormattingApplied = False
        End Try
    End If
End Sub

설명

CellFormatting 이벤트를 처리하여 셀 값의 변환을 표시에 적합한 형식으로 사용자 지정하거나 해당 상태 또는 값에 따라 셀 모양을 사용자 지정합니다.

이벤트는 CellFormatting 각 셀이 그려질 때마다 발생하므로 이 이벤트를 처리할 때 긴 처리를 피해야 합니다. 이 이벤트는 셀 FormattedValue 이 검색되거나 메서드 GetFormattedValue 가 호출될 때도 발생합니다.

이벤트를 처리 CellFormatting 하면 속성이 ConvertEventArgs.Value 셀 값으로 초기화됩니다. 셀 값에서 표시 값으로 사용자 지정 변환을 제공하는 경우 속성을 변환된 값으로 설정 ConvertEventArgs.Value 하여 새 값이 셀 FormattedValueType 속성에 지정된 형식인지 확인합니다. 더 이상 값 서식이 필요하지 않음을 나타내려면 속성을 trueDataGridViewCellFormattingEventArgs.FormattingApplied 설정합니다.

이벤트 처리기가 완료되면 가 올바른 형식이거나 이 아니거나 DataGridViewCellFormattingEventArgs.FormattingApplied 속성이 falseValueConvertEventArgs.Valuenull 는 셀 InheritedStyle 속성을 사용하여 초기화된 속성에서 반환 DataGridViewCellFormattingEventArgs.CellStyle 된 셀 스타일의 , NullValue, DataSourceNullValueFormatProvider 속성을 사용하여 Format형식이 지정됩니다.

의 값 DataGridViewCellFormattingEventArgs.FormattingApplied 에 관계 없이는 속성에서 반환 하는 개체의 표시 속성은 셀을 DataGridViewCellFormattingEventArgs.CellStyle 렌더링 하는 데 사용 됩니다.

이벤트를 사용한 CellFormatting 사용자 지정 서식 지정에 대한 자세한 내용은 방법: Windows Forms DataGridView 컨트롤에서 데이터 서식 사용자 지정을 참조하세요.

이 이벤트를 처리할 때 성능 저하를 방지하려면 셀에 직접 액세스하지 않고 이벤트 처리기의 매개 변수를 통해 셀에 액세스합니다.

서식이 지정된 사용자 지정 값을 실제 셀 값으로 변환하는 것을 사용자 지정하려면 이벤트를 처리합니다 CellParsing .

이벤트를 처리 하는 방법에 대 한 자세한 내용은 참조 하세요. 이벤트 처리 및 발생합니다.

DataGridViewCellFormattingEventHandler 대리자를 만들 때, 이벤트를 처리할 메서드를 식별합니다. 이벤트를 이벤트 처리기와 연결하려면 대리자의 인스턴스를 해당 이벤트에 추가합니다. 대리자를 제거하지 않는 경우 이벤트가 발생할 때마다 이벤트 처리기가 호출됩니다. 이벤트 처리기 대리자에 대 한 자세한 내용은 참조 하세요. 이벤트 처리 및 발생합니다.

확장 메서드

GetMethodInfo(Delegate)

지정된 대리자가 나타내는 메서드를 나타내는 개체를 가져옵니다.

적용 대상

추가 정보