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. 在此範例中, DataGridView.CellFormatting當資料格值等於DataSourceNullValue時, 事件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屬性的值, 在資料格中輸入 null 值。The user can enter a null value into a cell by pressing CTRL+0 or by typing the value of the NullValue property. 當使用者認可變更時, 基礎資料格的值會設定為DataSourceNullValue屬性null的值, 如果DataSourceNullValueDBNull.Value , 且資料格ValueType是參考型別, 則為。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.Value DataSourceNullValue 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.