DataGridViewCell.DefaultNewRowValue Property

Definition

Gets the default value for a cell in the row for new records.

C#
[System.ComponentModel.Browsable(false)]
public virtual object DefaultNewRowValue { get; }
C#
[System.ComponentModel.Browsable(false)]
public virtual object? DefaultNewRowValue { get; }

Property Value

An Object representing the default value.

Attributes

Examples

The following code example demonstrates how to override the DefaultNewRowValue property in a CalendarCell class that derives from DataGridViewTextBoxCell. This example is part of a larger code example provided in How to: Host Controls in Windows Forms DataGridView Cells.

C#
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;
        }
    }
}

Remarks

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. This value can be overridden by a handler for the DataGridView.DefaultValuesNeeded event when focus enters the row for new records.

Applies to

Produkt Verzie
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

See also