Share via


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

更新:2007 年 11 月

您在 DataGridView 控制項中顯示的資料,通常是來自某種資料來源,但是您可能會想要顯示並非來自資料來源的資料行。這種資料行稱為未繫結資料行。未繫結資料行可以使用多種表單,經常用來存取資料列的詳細資料。

下列程式碼範例示範如何建立 [詳細資料] 按鈕的未繫結資料行,以便在實作主從式 (Master/Detail) 案例時顯示父資料表中與特定資料列相關的子資料表。若要回應按鈕按下動作,請實作 DataGridView.CellClick 事件處理常式,此處理常式會顯示包含子資料表的表單。

Visual Studio 中會支援這項工作。

範例

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(1, 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(1, buttonColumn);
}

編譯程式碼

這項範例需要:

請參閱

概念

Windows Form DataGridView 控制項的資料顯示模式

參考

DataGridView

其他資源

在 Windows Form DataGridView 控制項中顯示資料