DataGridViewCell.DefaultNewRowValue 属性

定义

获取新记录所在行中单元格的默认值。

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

属性值

表示默认值的 Object

属性

示例

下面的代码示例演示如何重写DefaultNewRowValue派生自 DataGridViewTextBoxCell的类中的 CalendarCell 属性。 此示例是How to: Host Controls in dataGridView Cells中提供的更大代码示例的一部分,Windows 窗体。

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

注解

DefaultNewRowValue基类DataGridViewCell中的 属性始终返回 null。 但是,可以在派生单元格类中重写此属性以返回其他默认值。

如果单元格位于新记录的行中,则显示此属性返回的值。 当焦点进入新记录的行时, DataGridView.DefaultValuesNeeded 事件的处理程序可以重写此值。

适用于

产品 版本
.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

另请参阅