Redigera

Dela via


DataGridViewCellStyle.NullValue Property

Definition

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; }
[System.ComponentModel.TypeConverter(typeof(System.ComponentModel.StringConverter))]
public object? NullValue { get; set; }
[<System.ComponentModel.TypeConverter(typeof(System.ComponentModel.StringConverter))>]
member this.NullValue : obj with get, set
Public Property NullValue As Object

Property Value

The object used to indicate a null value in a cell. The default is Empty.

Attributes

Examples

The following code example illustrates the use of this property. 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

Remarks

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. 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. This conversion does not occur when you set the DataGridViewCell.Value property programmatically.

Note

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. 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.

Applies to

See also