Share via


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

更新:2007 年 11 月

您可以讓應用程式在新加入的資料列填入預設值,使資料輸入變得更方便。藉由 DataGridView 類別,您可使用 DefaultValuesNeeded 事件填入預設值。當使用者輸入新資料錄的資料列時,就會引發此事件。在程式碼處理這個事件時,您可以將選擇的值填入所要的儲存格。

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

範例

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
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 Form DataGridView 控制項中用於新增資料錄的資料列

參考

DataGridView

DataGridView.DefaultValuesNeeded

其他資源

Windows Form DataGridView 控制項中的資料輸入