DataGridView.DefaultValuesNeeded Event
Assembly: System.Windows.Forms (in system.windows.forms.dll)
This event populates the row for new records only when the user enters the row. Initial values for this row come from the DefaultNewRowValue property of the DataGridViewCell returned by each column's CellTemplate property.
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 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 columns in an actual DataGridView control and provide appropriate default values.
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["Region"].Value = "NA"; e.Row.Cells["Country"].Value = "USA"; e.Row.Cells["CustomerID"].Value = NewCustomerId(); }
Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.
Reference
DataGridView ClassDataGridView Members
System.Windows.Forms Namespace