如何:指定 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
編譯程式碼
這個範例需要:
名為
dataGridView1
的 DataGridView 控制項。用於產生唯
CustomerID
一NewCustomerId
值的函式。System 和 System.Windows.Forms 組件的參考。
另請參閱
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應