HOW TO:將未繫結的資料行加入至已資料繫結的 Windows Form DataGridView 控制項
您在 DataGridView 控制項中顯示的資料,通常是來自某種資料來源,但是您可能會想要顯示並非來自資料來源的資料行。 這種資料行稱為未繫結資料行。 未繫結資料行可以使用多種表單, 經常用來存取資料列的詳細資料。
下列程式碼範例示範如何建立 [詳細資料] 按鈕的未繫結資料行,以便在實作主從式 (Master/Detail) 案例時顯示父資料表中與特定資料列相關的子資料表。 若要回應按鈕按下動作,請實作 DataGridView.CellClick 事件處理常式,此處理常式會顯示包含子資料表的表單。
Visual Studio 中會支援這項工作。 如需詳細資訊,請參閱HOW TO:使用設計工具在 Windows Form DataGridView 控制項中加入和移除資料行 和 HOW TO:使用設計工具在 Windows Form DataGridView 控制項中加入和移除資料行 和 HOW TO:使用設計工具在 Windows Form DataGridView 控制項中加入和移除資料行 和 HOW TO:使用設計工具在 Windows Form DataGridView 控制項中加入和移除資料行.
範例
Private Sub CreateUnboundButtonColumn()
' Initialize the button column.
Dim buttonColumn As New DataGridViewButtonColumn
With buttonColumn
.HeaderText = "Details"
.Name = "Details"
.Text = "View Details"
' Use the Text property for the button text for all cells rather
' than using each cell's value as the text for its own button.
.UseColumnTextForButtonValue = True
End With
' Add the button column to the control.
dataGridView1.Columns.Insert(0, buttonColumn)
End Sub
private void CreateUnboundButtonColumn()
{
// Initialize the button column.
DataGridViewButtonColumn buttonColumn =
new DataGridViewButtonColumn();
buttonColumn.Name = "Details";
buttonColumn.HeaderText = "Details";
buttonColumn.Text = "View Details";
// Use the Text property for the button text for all cells rather
// than using each cell's value as the text for its own button.
buttonColumn.UseColumnTextForButtonValue = true;
// Add the button column to the control.
dataGridView1.Columns.Insert(0, buttonColumn);
}
編譯程式碼
這項範例需要:
名為 dataGridView1 的 DataGridView 控制項。
System 和 System.Windows.Forms 組件的參考。
請參閱
參考
概念
Windows Form DataGridView 控制項的資料顯示模式