DataGridViewCell.GetFormattedValue(Object, Int32, DataGridViewCellStyle, TypeConverter, TypeConverter, DataGridViewDataErrorContexts) 方法


获取为显示进行格式化的单元格的值。Gets the value of the cell as formatted for display.

 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);
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



要格式化的值。The value to be formatted.


该单元格的父行索引。The index of the cell's parent row.


对单元格有效的 DataGridViewCellStyleThe DataGridViewCellStyle in effect for the cell.


与值类型关联的 TypeConverter,它提供到格式化值类型的自定义转换;如果不需要此类自定义转换,则为 nullA TypeConverter associated with the value type that provides custom conversion to the formatted value type, or null if no such custom conversion is needed.


与格式化值类型相关联的 TypeConverter,它提供从该值类型进行的自定义转换;如果不需要这种自定义转换,则为 nullA TypeConverter associated with the formatted value type that provides custom conversion from the value type, or null if no such custom conversion is needed.


DataGridViewDataErrorContexts 值的按位组合,用于描述需要格式化值的上下文。A bitwise combination of DataGridViewDataErrorContexts values describing the context in which the formatted value is needed.



单元格的格式化值;如果该单元格不属于 DataGridView 控件,则为 nullThe formatted value of the cell or null if the cell does not belong to a DataGridView control.


格式化失败,这是由于 DataError 控件的 DataGridView 事件无处理程序或处理程序将 ThrowException 属性设置为 true 而引起的。Formatting failed and either there is no handler for the DataError event of the DataGridView control or the handler set the ThrowException property to true. 通常情况下,可将该异常对象强制转换为类型 FormatExceptionThe exception object can typically be cast to type FormatException.


DataGridView控件调用此方法以将单元值转换为属性指示的类型的等效显示值 FormattedValueTypeThe DataGridView control calls this method to convert a cell value into an equivalent display value of the type indicated by the FormattedValueType property. 控件在参数中将单元值传递到此方法 valueThe control passes the cell value to this method in the value parameter.

此方法引发 DataGridView.CellFormatting 事件。This method raises the DataGridView.CellFormatting event. 此事件的处理程序可以修改 valuecellStyle 参数。A handler for this event can modify both the value and cellStyle parameters. 但是,如果处理程序未将 DataGridViewCellFormattingEventArgs.FormattingApplied 属性设置为 true ,则此方法 value 使用对象的格式设置属性 cellStyleIf the handler does not set the DataGridViewCellFormattingEventArgs.FormattingApplied property to true, however, this method formats value using the formatting properties of the cellStyle object.


value DataGridViewCellStyle.NullValue DataGridViewCellStyle.DataSourceNullValueDataSourceNullValue 设置为或以外的值时,此方法不会转换为等于属性值的属性值 DBNull.Value nullThis method does not convert value to the DataGridViewCellStyle.NullValue property value for values equal to the DataGridViewCellStyle.DataSourceNullValue property value when DataSourceNullValue is set to a value other than DBNull.Value or null. 在这种情况下,可以处理 DataGridView.CellFormatting 事件以显示 NullValue 属性值。In this case, you can handle the DataGridView.CellFormatting event to display the NullValue property value. 有关详细信息,请参阅DataSourceNullValue中的代码示例。For more information, see the code example in DataSourceNullValue.

如果格式设置不成功,则 DataError 会发生该事件。If formatting is unsuccessful, the DataError event occurs. 如果此事件没有处理程序,或者处理程序将属性设置 DataGridViewDataErrorEventArgs.ThrowExceptiontrue ,则会引发异常。If there is no handler for this event or the handler sets the DataGridViewDataErrorEventArgs.ThrowException property to true, an exception is thrown.

如果参数包含值,则重写此方法的派生类通常将返回单元值的字符串表示形式 context ClipboardContentDerived classes that override this method will typically return a string representation of the cell value if the context argument includes the ClipboardContent value.