共用方式為


DataGridViewCell.DefaultNewRowValue 屬性

定義

取得新資料錄之資料列中儲存格的預設值。

public:
 virtual property System::Object ^ DefaultNewRowValue { System::Object ^ get(); };
[System.ComponentModel.Browsable(false)]
public virtual object DefaultNewRowValue { get; }
[System.ComponentModel.Browsable(false)]
public virtual object? DefaultNewRowValue { get; }
[<System.ComponentModel.Browsable(false)>]
member this.DefaultNewRowValue : obj
Public Overridable ReadOnly Property DefaultNewRowValue As Object

屬性值

Object,表示預設值。

屬性

範例

下列程式碼範例示範如何覆寫 DefaultNewRowValue 衍生自 DataGridViewTextBoxCell 的類別中的 CalendarCell 屬性。 此範例是How to: Host Controls in Windows Forms DataGridView Cells中提供的較大程式碼範例的一部分。

public class CalendarCell : DataGridViewTextBoxCell
{

    public CalendarCell()
        : base()
    {
        // Use the short date format.
        this.Style.Format = "d";
    }

    public override void InitializeEditingControl(int rowIndex, object 
        initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
    {
        // Set the value of the editing control to the current cell value.
        base.InitializeEditingControl(rowIndex, initialFormattedValue, 
            dataGridViewCellStyle);
        CalendarEditingControl ctl = 
            DataGridView.EditingControl as CalendarEditingControl;
        // Use the default row value when Value property is null.
        if (this.Value == null)
        {
            ctl.Value = (DateTime)this.DefaultNewRowValue;
        }
        else
        {
            ctl.Value = (DateTime)this.Value;
        }
    }

    public override Type EditType
    {
        get
        {
            // Return the type of the editing control that CalendarCell uses.
            return typeof(CalendarEditingControl);
        }
    }

    public override Type ValueType
    {
        get
        {
            // Return the type of the value that CalendarCell contains.
            
            return typeof(DateTime);
        }
    }

    public override object DefaultNewRowValue
    {
        get
        {
            // Use the current date and time as the default value.
            return DateTime.Now;
        }
    }
}
Public Class CalendarCell
    Inherits DataGridViewTextBoxCell

    Public Sub New()
        ' Use the short date format.
        Me.Style.Format = "d"
    End Sub

    Public Overrides Sub InitializeEditingControl(ByVal rowIndex As Integer, _
        ByVal initialFormattedValue As Object, _
        ByVal dataGridViewCellStyle As DataGridViewCellStyle)

        ' Set the value of the editing control to the current cell value.
        MyBase.InitializeEditingControl(rowIndex, initialFormattedValue, _
            dataGridViewCellStyle)

        Dim ctl As CalendarEditingControl = _
            CType(DataGridView.EditingControl, CalendarEditingControl)

        ' Use the default row value when Value property is null.
        If (Me.Value Is Nothing) Then
            ctl.Value = CType(Me.DefaultNewRowValue, DateTime)
        Else
            ctl.Value = CType(Me.Value, DateTime)
        End If
    End Sub

    Public Overrides ReadOnly Property EditType() As Type
        Get
            ' Return the type of the editing control that CalendarCell uses.
            Return GetType(CalendarEditingControl)
        End Get
    End Property

    Public Overrides ReadOnly Property ValueType() As Type
        Get
            ' Return the type of the value that CalendarCell contains.
            Return GetType(DateTime)
        End Get
    End Property

    Public Overrides ReadOnly Property DefaultNewRowValue() As Object
        Get
            ' Use the current date and time as the default value.
            Return DateTime.Now
        End Get
    End Property

End Class

備註

DefaultNewRowValue基類 DataGridViewCell 中的 屬性一律會傳 null 回 。 不過,這個屬性可以在衍生的資料格類別中覆寫,以傳回其他預設值。

如果儲存格位於新記錄的資料列中,則會顯示這個屬性所傳回的值。 當焦點進入新記錄的資料列時,事件處理常式 DataGridView.DefaultValuesNeeded 可以覆寫此值。

適用於

另請參閱