DataGridView Events


.NET Framework Class Library
DataGridView.DefaultValuesNeeded Event

Note: This event is new in the .NET Framework version 2.0.

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)

Syntax

Visual Basic (Declaration)
Public Event DefaultValuesNeeded As DataGridViewRowEventHandler
Visual Basic (Usage)
Dim instance As DataGridView
Dim handler As DataGridViewRowEventHandler

AddHandler instance.DefaultValuesNeeded, handler
C#
public event DataGridViewRowEventHandler DefaultValuesNeeded
C++
public:
event DataGridViewRowEventHandler^ DefaultValuesNeeded {
    void add (DataGridViewRowEventHandler^ value);
    void remove (DataGridViewRowEventHandler^ value);
}
J#
/** @event */
public void add_DefaultValuesNeeded (DataGridViewRowEventHandler value)

/** @event */
public void remove_DefaultValuesNeeded (DataGridViewRowEventHandler value)
JScript
JScript supports the use of events, but not the declaration of new ones.
Remarks

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.

Example

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.

Visual Basic
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("Region").Value = "NA"
        .Cells("Country").Value = "USA"
        .Cells("CustomerID").Value = NewCustomerId()
    End With

End Sub
C#
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();
}
Platforms

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.

Version Information

.NET Framework

Supported in: 2.0
See Also

Tags :


Page view tracker