DataGridViewButtonColumn 類別

定義

裝載 DataGridViewButtonCell 物件的集合。

public ref class DataGridViewButtonColumn : System::Windows::Forms::DataGridViewColumn
[System.Drawing.ToolboxBitmap(typeof(System.Windows.Forms.DataGridViewButtonColumn), "DataGridViewButtonColumn.bmp")]
public class DataGridViewButtonColumn : System.Windows.Forms.DataGridViewColumn
[System.Drawing.ToolboxBitmap(typeof(System.Windows.Forms.DataGridViewButtonColumn), "DataGridViewButtonColumn")]
public class DataGridViewButtonColumn : System.Windows.Forms.DataGridViewColumn
[<System.Drawing.ToolboxBitmap(typeof(System.Windows.Forms.DataGridViewButtonColumn), "DataGridViewButtonColumn.bmp")>]
type DataGridViewButtonColumn = class
    inherit DataGridViewColumn
[<System.Drawing.ToolboxBitmap(typeof(System.Windows.Forms.DataGridViewButtonColumn), "DataGridViewButtonColumn")>]
type DataGridViewButtonColumn = class
    inherit DataGridViewColumn
Public Class DataGridViewButtonColumn
Inherits DataGridViewColumn
繼承
屬性

範例

下列程式碼範例示範如何使用 DataGridViewButtonColumn ,在特定資料列上執行動作。 在此範例中 DataGridView.CellClick ,事件處理常式會先判斷按一下是否在按鈕資料格上,然後擷取與資料列相關聯的商務物件。 此範例是How to: Access Objects in a Windows Forms DataGridViewComboBoxCell Drop-Down List中可用的較大範例的一部分。

public class Form1 : Form
{
    private List<Employee> employees = new List<Employee>();
    private List<Task> tasks = new List<Task>();
    private Button reportButton = new Button();
    private DataGridView dataGridView1 = new DataGridView();

    [STAThread]
    public static void Main()
    {
        Application.Run(new Form1());
    }

    public Form1()
    {
        dataGridView1.Dock = DockStyle.Fill;
        dataGridView1.AutoSizeColumnsMode = 
            DataGridViewAutoSizeColumnsMode.AllCells;
        reportButton.Text = "Generate Report";
        reportButton.Dock = DockStyle.Top;
        reportButton.Click += new EventHandler(reportButton_Click);

        Controls.Add(dataGridView1);
        Controls.Add(reportButton);
        Load += new EventHandler(Form1_Load);
        Text = "DataGridViewComboBoxColumn Demo";
    }

    // Initializes the data source and populates the DataGridView control.
    private void Form1_Load(object sender, EventArgs e)
    {
        PopulateLists();
        dataGridView1.AutoGenerateColumns = false;
        dataGridView1.DataSource = tasks;
        AddColumns();
    }

    // Populates the employees and tasks lists. 
    private void PopulateLists()
    {
        employees.Add(new Employee("Harry"));
        employees.Add(new Employee("Sally"));
        employees.Add(new Employee("Roy"));
        employees.Add(new Employee("Pris"));
        tasks.Add(new Task(1, employees[1]));
        tasks.Add(new Task(2));
        tasks.Add(new Task(3, employees[2]));
        tasks.Add(new Task(4));
    }

    // Configures columns for the DataGridView control.
    private void AddColumns()
    {
        DataGridViewTextBoxColumn idColumn = 
            new DataGridViewTextBoxColumn();
        idColumn.Name = "Task";
        idColumn.DataPropertyName = "Id";
        idColumn.ReadOnly = true;

        DataGridViewComboBoxColumn assignedToColumn = 
            new DataGridViewComboBoxColumn();

        // Populate the combo box drop-down list with Employee objects. 
        foreach (Employee e in employees) assignedToColumn.Items.Add(e);

        // Add "unassigned" to the drop-down list and display it for 
        // empty AssignedTo values or when the user presses CTRL+0. 
        assignedToColumn.Items.Add("unassigned");
        assignedToColumn.DefaultCellStyle.NullValue = "unassigned";

        assignedToColumn.Name = "Assigned To";
        assignedToColumn.DataPropertyName = "AssignedTo";
        assignedToColumn.AutoComplete = true;
        assignedToColumn.DisplayMember = "Name";
        assignedToColumn.ValueMember = "Self";

        // Add a button column. 
        DataGridViewButtonColumn buttonColumn = 
            new DataGridViewButtonColumn();
        buttonColumn.HeaderText = "";
        buttonColumn.Name = "Status Request";
        buttonColumn.Text = "Request Status";
        buttonColumn.UseColumnTextForButtonValue = true;

        dataGridView1.Columns.Add(idColumn);
        dataGridView1.Columns.Add(assignedToColumn);
        dataGridView1.Columns.Add(buttonColumn);

        // Add a CellClick handler to handle clicks in the button column.
        dataGridView1.CellClick +=
            new DataGridViewCellEventHandler(dataGridView1_CellClick);
    }

    // Reports on task assignments. 
    private void reportButton_Click(object sender, EventArgs e)
    {
        StringBuilder report = new StringBuilder();
        foreach (Task t in tasks)
        {
            String assignment = 
                t.AssignedTo == null ? 
                "unassigned" : "assigned to " + t.AssignedTo.Name;
            report.AppendFormat("Task {0} is {1}.", t.Id, assignment);
            report.Append(Environment.NewLine);
        }
        MessageBox.Show(report.ToString(), "Task Assignments");
    }

    // Calls the Employee.RequestStatus method.
    void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
    {
        // Ignore clicks that are not on button cells. 
        if (e.RowIndex < 0 || e.ColumnIndex !=
            dataGridView1.Columns["Status Request"].Index) return;

        // Retrieve the task ID.
        Int32 taskID = (Int32)dataGridView1[0, e.RowIndex].Value;

        // Retrieve the Employee object from the "Assigned To" cell.
        Employee assignedTo = dataGridView1.Rows[e.RowIndex]
            .Cells["Assigned To"].Value as Employee;

        // Request status through the Employee object if present. 
        if (assignedTo != null)
        {
            assignedTo.RequestStatus(taskID);
        }
        else
        {
            MessageBox.Show(String.Format(
                "Task {0} is unassigned.", taskID), "Status Request");
        }
    }
}
Public Class Form1
    Inherits Form

    Private employees As New List(Of Employee)
    Private tasks As New List(Of Task)
    Private WithEvents reportButton As New Button
    Private WithEvents dataGridView1 As New DataGridView

    <STAThread()> _
    Public Sub Main()
        Application.Run(New Form1)
    End Sub

    Sub New()
        dataGridView1.Dock = DockStyle.Fill
        dataGridView1.AutoSizeColumnsMode = _
            DataGridViewAutoSizeColumnsMode.AllCells
        reportButton.Text = "Generate Report"
        reportButton.Dock = DockStyle.Top

        Controls.Add(dataGridView1)
        Controls.Add(reportButton)
        Text = "DataGridViewComboBoxColumn Demo"
    End Sub

    ' Initializes the data source and populates the DataGridView control.
    Private Sub Form1_Load(ByVal sender As Object, _
        ByVal e As EventArgs) Handles Me.Load

        PopulateLists()
        dataGridView1.AutoGenerateColumns = False
        dataGridView1.DataSource = tasks
        AddColumns()

    End Sub

    ' Populates the employees and tasks lists. 
    Private Sub PopulateLists()
        employees.Add(New Employee("Harry"))
        employees.Add(New Employee("Sally"))
        employees.Add(New Employee("Roy"))
        employees.Add(New Employee("Pris"))
        tasks.Add(New Task(1, employees(1)))
        tasks.Add(New Task(2))
        tasks.Add(New Task(3, employees(2)))
        tasks.Add(New Task(4))
    End Sub

    ' Configures columns for the DataGridView control.
    Private Sub AddColumns()

        Dim idColumn As New DataGridViewTextBoxColumn()
        idColumn.Name = "Task"
        idColumn.DataPropertyName = "Id"
        idColumn.ReadOnly = True

        Dim assignedToColumn As New DataGridViewComboBoxColumn()

        ' Populate the combo box drop-down list with Employee objects. 
        For Each e As Employee In employees
            assignedToColumn.Items.Add(e)
        Next

        ' Add "unassigned" to the drop-down list and display it for 
        ' empty AssignedTo values or when the user presses CTRL+0. 
        assignedToColumn.Items.Add("unassigned")
        assignedToColumn.DefaultCellStyle.NullValue = "unassigned"

        assignedToColumn.Name = "Assigned To"
        assignedToColumn.DataPropertyName = "AssignedTo"
        assignedToColumn.AutoComplete = True
        assignedToColumn.DisplayMember = "Name"
        assignedToColumn.ValueMember = "Self"

        ' Add a button column. 
        Dim buttonColumn As New DataGridViewButtonColumn()
        buttonColumn.HeaderText = ""
        buttonColumn.Name = "Status Request"
        buttonColumn.Text = "Request Status"
        buttonColumn.UseColumnTextForButtonValue = True

        dataGridView1.Columns.Add(idColumn)
        dataGridView1.Columns.Add(assignedToColumn)
        dataGridView1.Columns.Add(buttonColumn)

    End Sub

    ' Reports on task assignments. 
    Private Sub reportButton_Click(ByVal sender As Object, _
        ByVal e As EventArgs) Handles reportButton.Click

        Dim report As New StringBuilder()
        For Each t As Task In tasks
            Dim assignment As String
            If t.AssignedTo Is Nothing Then
                assignment = "unassigned"
            Else
                assignment = "assigned to " + t.AssignedTo.Name
            End If
            report.AppendFormat("Task {0} is {1}.", t.Id, assignment)
            report.Append(Environment.NewLine)
        Next
        MessageBox.Show(report.ToString(), "Task Assignments")

    End Sub

    ' Calls the Employee.RequestStatus method.
    Private Sub dataGridView1_CellClick(ByVal sender As Object, _
        ByVal e As DataGridViewCellEventArgs) _
        Handles dataGridView1.CellClick

        ' Ignore clicks that are not on button cells. 
        If e.RowIndex < 0 OrElse Not e.ColumnIndex = _
            dataGridView1.Columns("Status Request").Index Then Return

        ' Retrieve the task ID.
        Dim taskID As Int32 = CInt(dataGridView1(0, e.RowIndex).Value)

        ' Retrieve the Employee object from the "Assigned To" cell.
        Dim assignedTo As Employee = TryCast(dataGridView1.Rows(e.RowIndex) _
            .Cells("Assigned To").Value, Employee)

        ' Request status through the Employee object if present. 
        If assignedTo IsNot Nothing Then
            assignedTo.RequestStatus(taskID)
        Else
            MessageBox.Show(String.Format( _
                "Task {0} is unassigned.", taskID), "Status Request")
        End If

    End Sub

End Class

備註

類別 DataGridViewButtonColumn 是類別的特殊類型, DataGridViewColumn 用來以邏輯方式裝載回應簡單使用者輸入的儲存格。 DataGridViewButtonColumn在與它交集的每個 DataGridViewRow 中都有相關聯的 DataGridViewButtonCell 。 每個儲存格都會提供使用者介面 (UI) 類似于 Button 控制項。

若要顯示每個儲存格的相同按鈕文字,請將 UseColumnTextForButtonValue 屬性 true 設定為 ,並將 Text 屬性設定為所需的按鈕文字。

此資料行類型的預設排序模式為 NotSortable

若要回應使用者按鈕按一下,請處理 DataGridView.CellClickDataGridView.CellContentClick 事件。 在事件處理常式中 DataGridViewCellEventArgs.ColumnIndex ,您可以使用 屬性來判斷按一下是否發生在按鈕資料行中。 您可以使用 DataGridViewCellEventArgs.RowIndex 屬性來判斷按一下是否發生在按鈕資料格中,而不是在資料行標頭上。

注意

啟用視覺樣式時,按鈕資料行中的按鈕會使用 ButtonRenderer 繪製,而透過屬性 DefaultCellStyle 指定的儲存格樣式則沒有作用。

給繼承者的注意事項

當您衍生自 DataGridViewButtonColumn 並新增屬性至衍生類別時,請務必覆寫 Clone() 方法,以在複製作業期間複製新屬性。 您也應該呼叫基類 Clone() 的 方法,以便基類的屬性複製到新的儲存格。

建構函式

DataGridViewButtonColumn()

初始化 DataGridViewButtonColumn 類別的新執行個體為預設狀態。

屬性

AutoSizeMode

取得或設定資料行自動調整其寬度所根據的模式。

(繼承來源 DataGridViewColumn)
CellTemplate

取得或設定用來建立新儲存格的樣板。

CellType

取得儲存格樣板的執行階段類型。

(繼承來源 DataGridViewColumn)
ContextMenuStrip

取得或設定資料行的捷徑功能表。

(繼承來源 DataGridViewColumn)
DataGridView

取得與這個項目有關聯的 DataGridView 控制項。

(繼承來源 DataGridViewElement)
DataPropertyName

取得或設定 DataGridViewColumn 所繫結的資料來源屬性或資料庫資料行的名稱。

(繼承來源 DataGridViewColumn)
DefaultCellStyle

取得或設定資料行的預設儲存格樣式。

DefaultHeaderCellType

取得或設定預設標題儲存格的執行階段型別。

(繼承來源 DataGridViewBand)
Displayed

取得值,指出此群組列目前是否顯示在螢幕上。

(繼承來源 DataGridViewBand)
DisplayIndex

取得或設定相對於目前所顯示之資料行的資料行顯示順序。

(繼承來源 DataGridViewColumn)
DividerWidth

取得或設定資料行分割線的寬度 (以像素為單位)。

(繼承來源 DataGridViewColumn)
FillWeight

取得或設定值,表示處於填入模式中的資料行寬度,相對於控制項中處於填入模式的資料行寬度。

(繼承來源 DataGridViewColumn)
FlatStyle

取得或設定資料行中的按鈕儲存格的平面樣式外觀。

Frozen

取得或設定值,指出當使用者水平捲動 DataGridView 控制項時,資料行是否會跟著移動。

(繼承來源 DataGridViewColumn)
HasDefaultCellStyle

取得指出是否已經設定 DefaultCellStyle 屬性的值。

(繼承來源 DataGridViewBand)
HeaderCell

取得或設定表示資料行行首的 DataGridViewColumnHeaderCell

(繼承來源 DataGridViewColumn)
HeaderCellCore

取得或設定 DataGridViewBand 的標題儲存格。

(繼承來源 DataGridViewBand)
HeaderText

取得或設定資料行行首儲存格上的標題文字。

(繼承來源 DataGridViewColumn)
Index

取得 DataGridView 控制項內群組列的相對位置。

(繼承來源 DataGridViewBand)
InheritedAutoSizeMode

取得資料行的作用中調整大小模式。

(繼承來源 DataGridViewColumn)
InheritedStyle

取得目前套用至資料行的儲存格樣式。

(繼承來源 DataGridViewColumn)
IsDataBound

取得值指出資料行是否繫結至資料來源。

(繼承來源 DataGridViewColumn)
IsRow

取得值,指出群組列是否表示資料列。

(繼承來源 DataGridViewBand)
MinimumWidth

取得或設定資料行的最小寬度 (以像素為單位)。

(繼承來源 DataGridViewColumn)
Name

取得或設定資料行的名稱。

(繼承來源 DataGridViewColumn)
ReadOnly

取得或設定值,指出使用者是否可以編輯資料行的儲存格。

(繼承來源 DataGridViewColumn)
Resizable

取得或設定值,指出資料行是否可以重新調整大小。

(繼承來源 DataGridViewColumn)
Selected

取得或設定值,指出群組列是否位於已選取的使用者介面 (UI) 狀態下。

(繼承來源 DataGridViewBand)
Site

取得或設定資料行的站台。

(繼承來源 DataGridViewColumn)
SortMode

取得或設定資料行的排序模式。

(繼承來源 DataGridViewColumn)
State

取得此項目的使用者介面 (UI) 狀態。

(繼承來源 DataGridViewElement)
Tag

取得或設定物件,其中包含與群組列相關的資料。

(繼承來源 DataGridViewBand)
Text

取得或設定顯示於按鈕儲存格上的預設文字。

ToolTipText

取得或設定供工具提示使用的文字。

(繼承來源 DataGridViewColumn)
UseColumnTextForButtonValue

取得或設定值,指出 Text 屬性值是否要顯示為此資料行中的儲存格之按鈕文字。

ValueType

取得或設定資枓行儲存格中的值之資料類型。

(繼承來源 DataGridViewColumn)
Visible

取得或設定值,這個值指出是否看得到資料行。

(繼承來源 DataGridViewColumn)
Width

取得或設定資料行的目前寬度。

(繼承來源 DataGridViewColumn)

方法

Clone()

建立與這個資料行完全相同的複本。

Dispose()

釋放 DataGridViewBand 所使用的所有資源。

(繼承來源 DataGridViewBand)
Dispose(Boolean)

釋放 DataGridViewBand 所使用的 Unmanaged 資源,並選擇性地釋放 Managed 資源。

(繼承來源 DataGridViewColumn)
Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetPreferredWidth(DataGridViewAutoSizeColumnMode, Boolean)

根據指定的準則,計算資料行的理想寬度。

(繼承來源 DataGridViewColumn)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
OnDataGridViewChanged()

當群組列與不同的 DataGridView 產生關聯時呼叫。

(繼承來源 DataGridViewBand)
RaiseCellClick(DataGridViewCellEventArgs)

引發 CellClick 事件。

(繼承來源 DataGridViewElement)
RaiseCellContentClick(DataGridViewCellEventArgs)

引發 CellContentClick 事件。

(繼承來源 DataGridViewElement)
RaiseCellContentDoubleClick(DataGridViewCellEventArgs)

引發 CellContentDoubleClick 事件。

(繼承來源 DataGridViewElement)
RaiseCellValueChanged(DataGridViewCellEventArgs)

引發 CellValueChanged 事件。

(繼承來源 DataGridViewElement)
RaiseDataError(DataGridViewDataErrorEventArgs)

引發 DataError 事件。

(繼承來源 DataGridViewElement)
RaiseMouseWheel(MouseEventArgs)

引發 MouseWheel 事件。

(繼承來源 DataGridViewElement)
ToString()

取得描述資料行的字串。

事件

Disposed

發生於處置 (Dispose) DataGridViewColumn 時。

(繼承來源 DataGridViewColumn)

適用於

另請參閱