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 フォーム 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。 各セルには、コントロールに似た Button ユーザー インターフェイス (UI) が用意されています。

すべてのセルに同じボタン テキストを表示するには、 プロパティを UseColumnTextForButtonValuetrue 設定し、 プロパティを Text 目的のボタン テキストに設定します。

この列の種類の既定の並べ替えモードは です NotSortable

ユーザー ボタンのクリックに応答するには、 または DataGridView.CellContentClick イベントをDataGridView.CellClick処理します。 イベント ハンドラーでは、 プロパティを 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 によって使用されているアンマネージド リソースを解放し、オプションでマネージド リソースも解放します。

(継承元 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

DataGridViewColumn が破棄されたときに発生します。

(継承元 DataGridViewColumn)

適用対象

こちらもご覧ください