DataGridView.DefaultValuesNeeded 事件

定義

使用者在資料列中輸入新記錄以便填入預設值時發生。

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 

事件類型

範例

下列程式碼範例說明如何處理此事件。 在此範例中,指定資料行的儲存格會填入預設值。 針對資料 CustomerID 行,此值會從個別的方法擷取, (不會實作) 產生唯一的客戶識別碼。

若要執行此範例,請將資料行名稱取代為實際 DataGridView 控制項中的資料行物件名稱,並提供適當的預設值。 依名稱指定資料行時,您必須使用資料行物件的名稱,而不是資料行標題文字。

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

備註

此事件可讓您在使用者輸入資料列時填入新記錄的資料列。 資料列的初始值來自 DefaultNewRowValue 每個資料行的 屬性所傳回的 DataGridViewCellCellTemplate 屬性。

在資料系結模式中,資料系結資料行的所有資料格值都會儲存在外部資料源中。 當使用者輸入新記錄的資料列時,會在事件發生之前 DefaultValuesNeeded ,在資料來源中建立新的資料列。 當您在事件處理常式中填 DataGridViewRowEventArgs.Row 入 屬性時,這些值會直接新增至資料來源。

在虛擬模式中,發生此事件之後,就會 CellValuePushed 針對新資料列中的每個資料格發生事件,以便您將預設值儲存在自訂資料存放區中。 然後,針對 CellValueNeeded 新資料列中的每一個資料格,擷取您儲存在 CellValuePushed 事件中的值,然後隨即顯示。

如需如何處理事件的詳細資訊,請參閱 處理和引發事件

適用於

另請參閱