共用方式為


如何:指定 Windows Form DataGridView 控制項新資料列的預設值

當應用程式填入新加入資料列的預設值時,您可以讓資料輸入更加方便。 透過 類別 DataGridView ,您可以使用 事件填入預設值 DefaultValuesNeeded 。 當使用者輸入新記錄的資料列時,就會引發此事件。 當您的程式碼處理此事件時,您可以使用您選擇的值填入所需的儲存格。

下列程式碼範例示範如何使用 事件來指定新資料列 DefaultValuesNeeded 的預設值。

範例

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

編譯程式碼

這個範例需要:

另請參閱