当应用程序为新添加的行填充默认值时,能使数据输入变得更方便。通过 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