DataGridViewCellFormattingEventHandler 委托

定义

表示将处理 CellFormatting 事件 DataGridView的方法。

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

事件源。

示例

下面的代码示例演示如何处理事件 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 属性指定的类型。 若要指示不需要进一步的值格式,请将 DataGridViewCellFormattingEventArgs.FormattingApplied 属性设置为 true

事件处理程序完成后,如果ConvertEventArgs.Value属性的类型null不正确或不是正确的类型,则DataGridViewCellFormattingEventArgs.FormattingAppliedValuefalse使用由属性返回DataGridViewCellFormattingEventArgs.CellStyle的单元格样式的 、NullValueDataSourceNullValue属性和FormatProvider属性进行格式化Format,该属性使用单元格InheritedStyle属性初始化。

无论属性的值如何 DataGridViewCellFormattingEventArgs.FormattingApplied ,属性返回 DataGridViewCellFormattingEventArgs.CellStyle 的对象的显示属性都用于呈现单元格。

有关使用 CellFormatting 事件自定义格式的详细信息,请参阅 如何:在 Windows 窗体 DataGridView 控件中自定义数据格式

为了避免处理此事件时出现性能损失,请通过事件处理程序的参数(而不是直接访问单元格)访问单元格。

若要自定义格式化、用户指定的值转换为实际单元格值,请处理 CellParsing 该事件。

有关如何处理事件的详细信息,请参阅 处理和引发事件

创建 DataGridViewCellFormattingEventHandler 委托时,可以标识将处理事件的方法。 若要将事件与事件处理程序相关联,请将委托的实例添加到事件。 除非删除委托,否则每当事件发生时调用事件处理程序。 有关事件处理程序委托的详细信息,请参阅 处理和引发事件

扩展方法

名称 说明
GetMethodInfo(Delegate)

获取一个对象,该对象表示由指定委托表示的方法。

适用于

另请参阅