天天看點

如何:為 Windows 窗體 DataGridView 控件中的新行指定預設值

當應用程式為新添加的行填充預設值時,能使資料輸入變得更友善。通過 DataGridView 類,可以使用 DefaultValuesNeeded 事件填充預設值。此事件在使用者進入新記錄的行時引發。在代碼處理此事件時,可以用選擇的值填充所需的單元格。

下面的代碼示例示範如何使用 DefaultValuesNeeded 事件指定新行的預設值。

示例

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

繼續閱讀