DataGridViewCellStyle.DataSourceNullValue DataGridViewCellStyle.DataSourceNullValue DataGridViewCellStyle.DataSourceNullValue DataGridViewCellStyle.DataSourceNullValue Property


获取或设置用户在单元格中输入 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. 在此示例中, 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


用户可以通过按 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设置为属性的值; 如果DataSourceNullValueDBNull.Value , 并且单元nullValueType为引用类型, 则为。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. 以编程方式设置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.