DataGridViewCellStyle.DataSourceNullValue 屬性

定義

取得或設定當使用者輸入 null 值到儲存格時儲存到資料來源中的值。

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

屬性值

當使用者指定 null 儲存格值時儲存到資料來源中的值。 預設為 Value

屬性

範例

下列程式碼範例說明如何使用這個屬性。 在此範例中,當儲存格值等於 時, DataGridView.CellFormatting 事件處理常式會顯示 屬性的值 NullValueDataSourceNullValue

// 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 或輸入 屬性值,在儲存格中輸入 Null 值 NullValue 。 當使用者認可變更時,基礎資料格值會設定為 屬性的值 DataSourceNullValue ,或者 null 如果 DataSourceNullValueDBNull.Value 且儲存格 ValueType 是參考類型,則設定為 。 當您以程式設計方式設定 DataGridViewCell.Value 屬性時,不會進行這項轉換。

注意

當 設定為 或 null 以外的 DBNull.Value 值時 DataSourceNullValueNullValue 控制項不會顯示等於 DataSourceNullValue 屬性值之儲存格值的屬性值。 在此情況下,您可以處理 DataGridView.CellFormatting 事件以顯示 NullValue 屬性值。 如需詳細資訊,請參閱本主題中的程式碼範例。

適用於

另請參閱