DataGridViewCellFormattingEventArgs 类

定义

DataGridViewCellFormatting 事件提供数据。

public ref class DataGridViewCellFormattingEventArgs : System::Windows::Forms::ConvertEventArgs
public class DataGridViewCellFormattingEventArgs : System.Windows.Forms.ConvertEventArgs
type DataGridViewCellFormattingEventArgs = class
    inherit ConvertEventArgs
Public Class DataGridViewCellFormattingEventArgs
Inherits ConvertEventArgs
继承
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 属性指定的类型。 若要指示无需进一步设置值格式,请将 属性设置为 DataGridViewCellFormattingEventArgs.FormattingAppliedtrue

事件处理程序完成后,如果 ConvertEventArgs.Valuenull 或 不是正确的类型,或者 DataGridViewCellFormattingEventArgs.FormattingApplied 属性为 falseValue则使用 Format属性返回DataGridViewCellFormattingEventArgs.CellStyle的单元格样式的 、 NullValueDataSourceNullValueFormatProvider 属性(使用单元格InheritedStyle属性初始化)设置 格式。

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

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

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

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

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

构造函数

DataGridViewCellFormattingEventArgs(Int32, Int32, Object, Type, DataGridViewCellStyle)

初始化 DataGridViewCellFormattingEventArgs 类的新实例。

属性

CellStyle

获取或设置正在格式化的单元格的样式。

ColumnIndex

获取正在格式化的单元格的列索引。

DesiredType

获取所需值的数据类型。

(继承自 ConvertEventArgs)
FormattingApplied

获取或设置一个值,该值指示单元格值是否已成功格式化。

RowIndex

获取正在格式化的单元格的行索引。

Value

获取或设置 ConvertEventArgs 的值。

(继承自 ConvertEventArgs)

方法

Equals(Object)

确定指定对象是否等于当前对象。

(继承自 Object)
GetHashCode()

作为默认哈希函数。

(继承自 Object)
GetType()

获取当前实例的 Type

(继承自 Object)
MemberwiseClone()

创建当前 Object 的浅表副本。

(继承自 Object)
ToString()

返回表示当前对象的字符串。

(继承自 Object)

适用于

另请参阅