DataGridViewButtonColumn クラス
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
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.bmp")>]
type DataGridViewButtonColumn = class
inherit DataGridViewColumn
Public Class DataGridViewButtonColumn
Inherits DataGridViewColumn
- 継承
- 属性
例
次のコード例では、 DataGridViewButtonColumn を使用して特定の行に対してアクションを実行する方法を示します。 この例では、 DataGridView.CellClick イベント ハンドラーは、最初にクリックがボタン セルにあるかどうかを判断し、その行に関連付けられているビジネス オブジェクトを取得します。 この例は、「 方法: Windows フォーム DataGridViewComboBoxCell Drop-Down リスト内のオブジェクトにアクセスする」で使用できるより大きな例の一部です。
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には、交差するすべてのDataGridViewButtonCellに関連付けられたDataGridViewRowがあります。 各セルには、 Button コントロールに似たユーザー インターフェイス (UI) が用意されています。
すべてのセルに同じボタン テキストを表示するには、 UseColumnTextForButtonValue プロパティを true に設定し、 Text プロパティを目的のボタン テキストに設定します。
この列の種類の既定の並べ替えモードは NotSortable。
ユーザー ボタンのクリックに応答するには、 DataGridView.CellClick または DataGridView.CellContentClick イベントを処理します。 イベント ハンドラーでは、 DataGridViewCellEventArgs.ColumnIndex プロパティを使用して、ボタン列でクリックが発生したかどうかを確認できます。 DataGridViewCellEventArgs.RowIndex プロパティを使用すると、列ヘッダーではなくボタン セルでクリックが発生したかどうかを判断できます。
Note
ビジュアル スタイルが有効になっている場合、ボタン列のボタンは 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) |
メソッド
イベント
| 名前 | 説明 |
|---|---|
| Disposed |
DataGridViewColumnが破棄されるときに発生します。 (継承元 DataGridViewColumn) |