DataGridViewCellStyle.NullValue 屬性

定義

取得或設定 DataGridView 儲存格顯示值,這個值會對應到 Value 的儲存格值或 null

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

屬性值

用來表示儲存格中的 null 值的物件。 預設為 Empty

屬性

範例

下列程式碼範例說明如何使用這個屬性。 在此範例中,當儲存格值等於 時, 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

備註

DataGridView當具有此儲存格樣式的儲存格具有 或 的值 DBNull.Valuenull 或使用者編輯儲存格並按 CTRL+0 時, DataGridView 控制項會顯示 NullValue 屬性值。 當使用者編輯具有此儲存格樣式的儲存格,並輸入此屬性的值或按下 CTRL+0 時,控制項會將儲存格值設定為 屬性的值 DataSourceNullValue ,如果 nullDataSourceNullValueDBNull.Value ,而且儲存格 ValueType 是參考類型。 當您以程式設計方式設定 DataGridViewCell.Value 屬性時,不會進行這項轉換。

注意

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

這個屬性會接受任何 物件,這可讓您指定具有適合儲存格顯示類型之類型的值。 例如,您可以將此屬性設定為字串值,以供文字方塊儲存格使用,或影像儲存格使用影像。

適用於

另請參閱