DataGridView.DefaultValuesNeeded 事件

定义

用户输入新记录行以便可以使用默认值填充该行时发生。

public:
 event System::Windows::Forms::DataGridViewRowEventHandler ^ DefaultValuesNeeded;
public event System.Windows.Forms.DataGridViewRowEventHandler DefaultValuesNeeded;
public event System.Windows.Forms.DataGridViewRowEventHandler? DefaultValuesNeeded;
member this.DefaultValuesNeeded : System.Windows.Forms.DataGridViewRowEventHandler 
Public Custom Event DefaultValuesNeeded As DataGridViewRowEventHandler 

事件类型

示例

下面的代码示例演示了如何处理此事件。 在此示例中,给定列的单元格使用默认值填充。 CustomerID对于 列,该值从生成唯一客户 ID 的单独方法检索 (未实现) 。

若要运行此示例,请将列名称替换为实际 DataGridView 控件中的列对象的名称,并提供适当的默认值。 按名称指定列时,必须使用列对象的名称,而不是列标题文本。

private void dataGridView1_DefaultValuesNeeded(object sender,
    System.Windows.Forms.DataGridViewRowEventArgs e)
{
    e.Row.Cells["Region"].Value = "WA";
    e.Row.Cells["City"].Value = "Redmond";
    e.Row.Cells["PostalCode"].Value = "98052-6399";
    e.Row.Cells["Country"].Value = "USA";
    e.Row.Cells["CustomerID"].Value = NewCustomerId();
}
Private Sub dataGridView1_DefaultValuesNeeded(ByVal sender As Object, _
    ByVal e As System.Windows.Forms.DataGridViewRowEventArgs) _
    Handles dataGridView1.DefaultValuesNeeded

    With e.Row
        .Cells("Region").Value = "WA"
        .Cells("City").Value = "Redmond"
        .Cells("PostalCode").Value = "98052-6399"
        .Cells("Country").Value = "USA"
        .Cells("CustomerID").Value = NewCustomerId()
    End With

End Sub

注解

通过此事件,你可以在用户输入行时填充新记录的行。 行的初始值来自 DefaultNewRowValue 每个列的 属性 DataGridViewCell 返回的 CellTemplate 的 属性。

在数据绑定模式下,数据绑定列的所有单元格值都存储在外部数据源中。 当用户输入新记录的行时,在事件发生之前 DefaultValuesNeeded ,数据源中会创建一个新行。 在事件处理程序中填充 DataGridViewRowEventArgs.Row 属性时,值将直接添加到数据源。

在虚拟模式下,发生此事件后, CellValuePushed 新行中的每个单元格都会发生 该事件,以便您可以将默认值存储在自定义数据存储中。 然后, CellValueNeeded 新行中的每个单元格都会发生 该事件,检索存储在 事件中的 CellValuePushed 值,然后显示这些值。

有关如何处理事件的详细信息,请参阅 处理和引发事件

适用于

另请参阅