DataGridView.DefaultValuesNeeded 事件


用户输入新记录行以便可以使用默认值填充该行时发生。Occurs when the user enters the row for new records so that it can be populated with default values.

 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 


下面的代码示例演示如何处理此事件。The following code example illustrates how this event can be handled. 在此示例中,给定列的单元格用默认值填充。In the example, cells for the given columns are populated with default values. 对于 CustomerID 列,将从生成唯一客户 ID 的单独方法(未实现)中检索值。For the CustomerID column, the value is retrieved from a separate method (not implemented) that generates a unique customer ID.

若要运行此示例,请将列名替换为实际 @no__t 0 控件中列对象的名称,并提供适当的默认值。To run this example, replace the column names with the names of the column objects from an actual DataGridView control and provide appropriate default values. 按名称指定列时,必须使用列对象的名称,而不是列标题文本。When specifying columns by name, you must use the name of the column object and not the column header text.

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


此事件允许您在用户输入行时填充新记录行。This event lets you populate the row for new records when the user enters the row. 行的初始值来自每个列的 CellTemplate 属性返回的 @no__t 的 @no__t 0 属性。Initial values for the row come from the DefaultNewRowValue property of the DataGridViewCell returned by each column's CellTemplate property.

在数据绑定模式下,数据绑定列的所有单元值都存储在外部数据源中。In data bound mode, all cell values for data bound columns are stored in the external data source. 当用户输入新记录行时,在 DefaultValuesNeeded 事件发生之前,将在数据源中创建新行。When the user enters the row for new records, a new row is created in the data source before the DefaultValuesNeeded event occurs. 在事件处理程序中填充 DataGridViewRowEventArgs.Row 属性时,会将值直接添加到数据源。When you populate the DataGridViewRowEventArgs.Row property in your event handler, the values are added directly to the data source.

在虚拟模式下,此事件发生后,为新行中的每个单元格执行 CellValuePushed 事件,以便可以将默认值存储在自定义数据存储中。In virtual mode, after this event occurs, the CellValuePushed event occurs for each cell in the new row so that you can store the default values in your custom data store. 然后,为新行中的每个单元格执行 CellValueNeeded 事件,检索存储在 @no__t 1 事件中的值,然后显示这些值。Then, the CellValueNeeded event occurs for each cell in the new row, retrieving the values that you stored in the CellValuePushed event, which are then displayed.

有关如何处理事件的详细信息,请参阅处理和引发事件For more information about how to handle events, see Handling and Raising Events.