DataGridViewCellStyle.DataSourceNullValue 属性


获取或设置用户在单元格中输入 null 值时保存到数据源的值。Gets or sets the value saved to the data source when the user enters a null value into a cell.

 property System::Object ^ DataSourceNullValue { System::Object ^ get(); void set(System::Object ^ value); };
public object DataSourceNullValue { get; set; }
member this.DataSourceNullValue : obj with get, set
Public Property DataSourceNullValue As Object


当用户输入 null 单元格值时保存到数据源的值。The value saved to the data source when the user specifies a null cell value. 默认值为 ValueThe default is Value.



下面的代码示例演示如何使用此属性。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


用户可以通过按 CTRL + 0 或键入 NullValue 属性的值,在单元格中输入空值。The user can enter a null value into a cell by pressing CTRL+0 or by typing the value of the NullValue property. 当用户提交更改时,基础单元值将设置为 DataSourceNullValue 属性的值,或者,如果 DataSourceNullValue DBNull.Value 并且单元 ValueType 是引用类型,则为 nullWhen 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. 如果以编程方式设置 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.