DataGridViewCellFormattingEventArgs 類別

定義

提供 CellFormattingDataGridView 事件的資料。

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 方法被呼叫時,也會發生這個事件。

當您處理事件時 CellFormattingConvertEventArgs.Value 屬性會以資料格值初始化。 如果您提供從儲存格值到顯示值的自訂轉換,請將 ConvertEventArgs.Value 屬性設定為已轉換的值,確保新值是儲存格 FormattedValueType 屬性所指定的類型。 若要指出不需要進一步的值格式設定,請將 DataGridViewCellFormattingEventArgs.FormattingApplied 屬性設定為 true

當事件處理常式完成時,如果 ConvertEventArgs.Valuenull 或 不是正確的型別,或 DataGridViewCellFormattingEventArgs.FormattingApplied 屬性為 false ,則會 Value 使用 Format 屬性所 DataGridViewCellFormattingEventArgs.CellStyle 傳回之儲存格樣式的 、 DataSourceNullValueNullValue 、 和 FormatProvider 屬性來格式化,而此屬性會使用儲存格 InheritedStyle 屬性初始化。

不論 屬性的值 DataGridViewCellFormattingEventArgs.FormattingApplied 為何,屬性所 DataGridViewCellFormattingEventArgs.CellStyle 傳回物件的顯示內容都會用來呈現儲存格。

如需使用 CellFormatting 事件自訂格式的詳細資訊,請參閱How to: Customize Data Formatting in the Windows Forms DataGridView Control

若要避免處理此事件時的效能損失,請透過事件處理常式的參數存取資料格,而不是直接存取資料格。

若要自訂格式化的使用者指定值轉換成實際儲存格值,請處理 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)

適用於

另請參閱