DataGridViewCellStyle.NullValue 属性


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

 property System::Object ^ NullValue { System::Object ^ get(); void set(System::Object ^ value); };
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. 在此示例中, DataGridView.CellFormatting事件处理程序在单元格值等于NullValue DataSourceNullValue时显示属性的值。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.Value null当具有此单元格样式的NullValue单元格的值为或或用户编辑单元格并按 CTRL + 0 时, 控件将显示属性值。 DataGridViewWhen 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设置为属性的值null ; 如果DataSourceNullValueDBNull.Value , 则该单元格ValueType为引用类型。When 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.


NullValue当设置为DBNull.ValueDataSourceNullValue DataSourceNullValue 以外null的值时, 控件不会显示等于属性值的单元值的属性值。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.