DataGridViewCellStyle.NullValue DataGridViewCellStyle.NullValue DataGridViewCellStyle.NullValue DataGridViewCellStyle.NullValue Property

定義

取得或設定 DataGridView 儲存格顯示值,這個值會對應到 Value 的儲存格值或 nullGets or sets the DataGridView cell display value corresponding to a cell value of Value or 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; }
member this.NullValue : obj with get, set
Public Property NullValue As Object

屬性值

用來表示儲存格中的 null 值的物件。The object used to indicate a null value in a cell. 預設為 EmptyThe default is Empty.

範例

下列程式碼範例說明如何使用這個屬性。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

備註

DataGridView DBNull.Value null當具有此儲存格樣式的NullValue儲存格具有或的值, 或使用者編輯資料格並按下 CTRL + 0 時, 控制項會顯示內容值。 DataGridViewWhen a DataGridView cell with this cell style has a value of DBNull.Value or null or the user edits the cell and presses CTRL+0, the DataGridView control displays the NullValue property value. 當使用者編輯具有此儲存格樣式的儲存格, 並輸入此屬性的值或按 CTRL + 0 時, 控制項會將資料格值設定為DataSourceNullValue屬性null的值, 如果DataSourceNullValueDBNull.Value , 則資料格ValueType為參考型別。When a user edits a cell with this cell style and enters the value of this property or presses CTRL+0, the control sets the cell value 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.

這個屬性會採用任何物件, 可讓您指定一個值, 其類型適用于儲存格的顯示類型。This property takes any object, which enables you to specify a value with a type appropriate to the display type of the cell. 例如, 您可以將此屬性設定為字串值, 供文字方塊儲存格或影像用於影像資料格使用。For example, you can set this property to string values for use by text box cells or images for use by image cells.

適用於

另請參閱