This documentation is archived and is not being maintained.

DataGridView.DefaultValuesNeeded Event

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

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

public event DataGridViewRowEventHandler DefaultValuesNeeded

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 handling events, see Consuming Events.

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();

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0