Редагувати

Поділитися через


DataGridViewCellStyle.DataSourceNullValue Property

Definition

Gets or sets the value saved to the data source when the user enters a null value into a cell.

public:
 property System::Object ^ DataSourceNullValue { System::Object ^ get(); void set(System::Object ^ value); };
[System.ComponentModel.Browsable(false)]
public object DataSourceNullValue { get; set; }
[System.ComponentModel.Browsable(false)]
public object? DataSourceNullValue { get; set; }
[<System.ComponentModel.Browsable(false)>]
member this.DataSourceNullValue : obj with get, set
Public Property DataSourceNullValue As Object

Property Value

The value saved to the data source when the user specifies a null cell value. The default is Value.

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

The user can enter a null value into a cell by pressing CTRL+0 or by typing the value of the NullValue property. When the user commits the change, the underlying cell value is set 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.

Applies to

See also