DataGridViewCell.DefaultNewRowValue プロパティ

定義

新しいレコードの行のセルに対する既定値を取得します。Gets the default value for a cell in the row for new records.

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

プロパティ値

既定値を表す ObjectAn Object representing the default value.

属性

次のコード例は、DataGridViewTextBoxCellから派生する CalendarCell クラスの DefaultNewRowValue プロパティをオーバーライドする方法を示しています。The following code example demonstrates how to override the DefaultNewRowValue property in a CalendarCell class that derives from DataGridViewTextBoxCell. この例は 次の方法で提供されるより大きなコード例の一部です。DataGridView セルWindows フォームのコントロールをホストします。This example is part of a larger code example provided in 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

注釈

基本クラス DataGridViewCellDefaultNewRowValue プロパティは、常に nullを返します。The DefaultNewRowValue property in the base class DataGridViewCell always returns null. ただし、このプロパティは、派生セルクラスでオーバーライドして、他の既定値を返すことができます。However, this property can be overridden in derived cell classes to return other default values.

新しいレコードの行にセルがある場合は、このプロパティによって返される値が表示されます。The value returned by this property is displayed if the cell is in the row for new records. この値は、新しいレコードの行にフォーカスが入ったときに、DataGridView.DefaultValuesNeeded イベントのハンドラーによってオーバーライドされる可能性があります。This value can be overridden by a handler for the DataGridView.DefaultValuesNeeded event when focus enters the row for new records.

適用対象

こちらもご覧ください