DataGridViewCell.GetFormattedValue 方法

定義

取得儲存格的格式化值,以供顯示。

protected:
 virtual System::Object ^ GetFormattedValue(System::Object ^ value, int rowIndex, System::Windows::Forms::DataGridViewCellStyle ^ % cellStyle, System::ComponentModel::TypeConverter ^ valueTypeConverter, System::ComponentModel::TypeConverter ^ formattedValueTypeConverter, System::Windows::Forms::DataGridViewDataErrorContexts context);
protected virtual object GetFormattedValue (object value, int rowIndex, ref System.Windows.Forms.DataGridViewCellStyle cellStyle, System.ComponentModel.TypeConverter valueTypeConverter, System.ComponentModel.TypeConverter formattedValueTypeConverter, System.Windows.Forms.DataGridViewDataErrorContexts context);
protected virtual object? GetFormattedValue (object? value, int rowIndex, ref System.Windows.Forms.DataGridViewCellStyle cellStyle, System.ComponentModel.TypeConverter? valueTypeConverter, System.ComponentModel.TypeConverter? formattedValueTypeConverter, System.Windows.Forms.DataGridViewDataErrorContexts context);
abstract member GetFormattedValue : obj * int * DataGridViewCellStyle * System.ComponentModel.TypeConverter * System.ComponentModel.TypeConverter * System.Windows.Forms.DataGridViewDataErrorContexts -> obj
override this.GetFormattedValue : obj * int * DataGridViewCellStyle * System.ComponentModel.TypeConverter * System.ComponentModel.TypeConverter * System.Windows.Forms.DataGridViewDataErrorContexts -> obj
Protected Overridable Function GetFormattedValue (value As Object, rowIndex As Integer, ByRef cellStyle As DataGridViewCellStyle, valueTypeConverter As TypeConverter, formattedValueTypeConverter As TypeConverter, context As DataGridViewDataErrorContexts) As Object

參數

value
Object

要格式化的值。

rowIndex
Int32

儲存格的父資料列索引。

cellStyle
DataGridViewCellStyle

DataGridViewCellStyle 對儲存格有作用。

valueTypeConverter
TypeConverter

與實值型別相關的 TypeConverter,此實值型別可提供轉換為格式化實值型別的自訂轉換作業,如果不需要這種自訂轉換,則為 null

formattedValueTypeConverter
TypeConverter

與格式化實值型別相關的 TypeConverter,此格式化實值型別可提供從實值型別轉換的自訂轉換作業,如果不需要這種自訂轉換,則為 null

context
DataGridViewDataErrorContexts

DataGridViewDataErrorContexts 值的位元 (Bitwise) 組合,其描述需要此格式化值的內容。

傳回

如果儲存格不屬於 DataGridView 控制項,則為儲存格的已格式化值或 null

例外狀況

格式化失敗,而且 DataError 控制項的 DataGridView 事件沒有處理常式,或是該處理常式將 ThrowException 屬性設定為 true。 例外狀況物件通常可轉換成 FormatException 類型。

備註

控制項 DataGridView 會呼叫這個方法,將儲存格值轉換成 屬性所 FormattedValueType 指示之類型的對等顯示值。 控制項會將儲存格值傳遞至 參數中的 value 這個方法。

這個方法會 DataGridView.CellFormatting 引發 事件。 這個事件的處理常式可以同時修改 valuecellStyle 參數。 不過,如果處理常式未將 DataGridViewCellFormattingEventArgs.FormattingApplied 屬性設定為 true ,則此方法會使用 物件的格式化屬性 cellStyle 來格式化 value

注意

當 設定為 DataGridViewCellStyle.NullValuenull 以外的 DBNull.ValueDataGridViewCellStyle.DataSourceNullValueDataSourceNullValue ,這個方法不會轉換成 value 等於屬性值的屬性值。 在此情況下,您可以處理 DataGridView.CellFormatting 事件以顯示 NullValue 屬性值。 如需詳細資訊,請參閱DataSourceNullValue 中的程式碼範例。

如果格式設定失敗,就會 DataError 發生此事件。 如果這個事件沒有處理常式,或處理常式會將 DataGridViewDataErrorEventArgs.ThrowException 屬性設定為 true ,則會擲回例外狀況。

如果引數包含 ClipboardContent 值,覆寫這個方法的衍生類別通常會傳回儲存格值的 context 字串表示。

適用於

另請參閱