Redigeeri

Jagamisviis:


DataGridView.DefaultValuesNeeded Event

Definition

Occurs when the user enters the row for new records so that it can be populated with default values.

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 

Event Type

Examples

The following code example illustrates how this event can be handled. In the example, cells for the given columns are populated with default values. For the CustomerID column, the value is retrieved from a separate method (not implemented) that generates a unique customer ID.

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

Remarks

This event lets you populate the row for new records when the user enters the row. 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. When the user enters the row for new records, a new row is created in the data source before the DefaultValuesNeeded event occurs. When you populate the DataGridViewRowEventArgs.Row property in your event handler, the values are added directly to the data source.

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. 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.

Applies to

See also