DataGridViewCellStyle.NullValue 属性

定义

获取或设置与 DataGridViewValue 的单元格值对应的 null 单元格显示值。Gets or sets the DataGridView cell display value corresponding to a cell value of Value or null.

public:
 property System::Object ^ NullValue { System::Object ^ get(); void set(System::Object ^ value); };
[System.ComponentModel.TypeConverter(typeof(System.ComponentModel.StringConverter))]
public object NullValue { get; set; }
member this.NullValue : obj with get, set
Public Property NullValue As Object

属性值

用于指示单元格为 null 值的对象。The object used to indicate a null value in a cell. 默认值为 EmptyThe default is Empty.

属性

示例

下面的代码示例演示如何使用此属性。The following code example illustrates the use of this property. 在此示例中,当单元格值等于 DataSourceNullValue时,DataGridView.CellFormatting 事件处理程序将显示 NullValue 属性的值。In this example, a DataGridView.CellFormatting event handler displays the value of the NullValue property when the cell value equals DataSourceNullValue.

// Display NullValue for cell values equal to DataSourceNullValue.
private void dataGridView1_CellFormatting(object sender,
    DataGridViewCellFormattingEventArgs e)
{
    String value = e.Value as string;
    if ((value != null) && value.Equals(e.CellStyle.DataSourceNullValue))
    {
        e.Value = e.CellStyle.NullValue;
        e.FormattingApplied = true;
    }
}
' Display NullValue for cell values equal to DataSourceNullValue.
Private Sub dataGridView1_CellFormatting(ByVal sender As Object, _
    ByVal e As DataGridViewCellFormattingEventArgs) _
    Handles dataGridView1.CellFormatting

    Dim value As String = TryCast(e.Value, String)
    If value IsNot Nothing And _
        value.Equals(e.CellStyle.DataSourceNullValue) Then

        e.Value = e.CellStyle.NullValue
        e.FormattingApplied = True

    End If

End Sub

注解

当具有此单元格样式的 DataGridView 单元格的值为 DBNull.Valuenull 或用户编辑单元格并按 CTRL + 0 时,DataGridView 控件将显示 NullValue 属性值。When a DataGridView cell with this cell style has a value of DBNull.Value or null or the user edits the cell and presses CTRL+0, the DataGridView control displays the NullValue property value. 当用户编辑具有此单元格样式的单元格,或按 CTRL + 0 时,该控件将单元格值设置为 DataSourceNullValue 属性的值,或者,如果 DataSourceNullValue DBNull.Value 并且单元 ValueType 是引用类型,则为 nullWhen a user edits a cell with this cell style and enters the value of this property or presses CTRL+0, the control sets the cell value to the value of the DataSourceNullValue property or to null if DataSourceNullValue is DBNull.Value and the cell ValueType is a reference type. 如果以编程方式设置 DataGridViewCell.Value 属性,则不会发生此转换。This conversion does not occur when you set the DataGridViewCell.Value property programmatically.

备注

DataSourceNullValue 设置为 DBNull.Valuenull以外的值时,控件不会显示等于 DataSourceNullValue 属性值的单元值的 NullValue 属性值。The control does not display the NullValue property value for cell values equal to the 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. 有关详细信息,请参阅本主题中的代码示例。For more information, see the code example in this topic.

此属性采用任何对象,您可以使用该对象指定类型与单元格的显示类型相应的值。This property takes any object, which enables you to specify a value with a type appropriate to the display type of the cell. 例如,您可以将此属性设置为字符串值,供文本框单元或图像使用以供图像单元使用。For example, you can set this property to string values for use by text box cells or images for use by image cells.

适用于

另请参阅