如何:將未繫結的資料行加入至已資料繫結的 Windows Form DataGridView 控制項

您在 DataGridView 控制項中顯示的資料,通常來自於某種的資料來源,但您可能想要顯示不是來自資料來源之資料的資料行。 這種資料行稱為未繫結資料行。 未繫結資料行可以有許多形式。 通常,它們用來提供資料列詳細資料的存取權。

下列程式碼範例示範如何在實作主要/詳細資料案例時,建立 [詳細 資料] 按鈕的 未系結資料行,以顯示與父資料表中特定資料列相關的子資料工作表。 若要回應按下按鈕後,實作 DataGridView.CellClick 事件處理常式,其顯示包含子資料表的表單。

在 Visual Studio 中會支援這項工作。 另請參閱 如何:使用設計 工具在 Windows Forms DataGridView 控制項中新增和移除資料行。

範例

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);
}
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

編譯程式碼

這個範例需要:

另請參閱