How to: Specify Default Values for New Rows in the Windows Forms DataGridView Control

 

You can make data entry more convenient when the application fills in default values for newly added rows. With the DataGridView class, you can fill in default values with the DefaultValuesNeeded event. This event is raised when the user enters the row for new records. When your code handles this event, you can populate desired cells with values of your choosing.

The following code example demonstrates how to specify default values for new rows using the DefaultValuesNeeded event.

Example

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

Compiling the Code

This example requires:

Show: