DataGridViewButtonCell クラス
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
DataGridView コントロールで使用するボタンのようなユーザー インターフェイス (UI) を表示します。
public ref class DataGridViewButtonCell : System::Windows::Forms::DataGridViewCell
public class DataGridViewButtonCell : System.Windows.Forms.DataGridViewCell
type DataGridViewButtonCell = class
inherit DataGridViewCell
Public Class DataGridViewButtonCell
Inherits DataGridViewCell
- 継承
例
次のコード例では、 DataGridViewButtonColumn を使用して特定の行に対してアクションを実行する方法を示します。 個々の DataGridViewButtonCell オブジェクトを操作する場合も同様のコードを使用できます。 この例では、 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
注釈
DataGridViewButtonCell クラスは、ボタンのような UI を表示するために使用される特殊な種類のDataGridViewCellです。
DataGridViewButtonColumn は、この型のセルを保持するために特化された列型です。 既定では、 DataGridViewButtonColumn.CellTemplate は新しい DataGridViewButtonCellに初期化されます。 既存の DataGridViewButtonCellの後に列内のセルをパターン化するには、パターンとして使用するセルに列の CellTemplate プロパティを設定します。
ユーザー ボタンのクリックに応答するには、 DataGridView.CellClick または DataGridView.CellContentClick イベントを処理します。 イベント ハンドラーでは、 DataGridViewCellEventArgs.ColumnIndex プロパティを使用して、クリックによってボタン列が発生したかどうかを判断できます。 DataGridViewCellEventArgs.RowIndex プロパティを使用して、クリックが特定のボタン セルで発生したかどうかを判断できます。
列のセル関連プロパティは、テンプレート セルの同様の名前のプロパティのラッパーです。 テンプレート セルのプロパティ値を変更すると、変更後に追加されたテンプレートに基づくセルにのみ影響します。 ただし、列のセル関連のプロパティ値を変更すると、テンプレート セルと列内の他のすべてのセルが更新され、必要に応じて列の表示が更新されます。
Note
ビジュアル スタイルが有効になっている場合、ボタン列のボタンは ButtonRendererを使用して描画され、 DefaultCellStyle などのプロパティで指定されたセル スタイルは効果がありません。
注意 (継承者)
DataGridViewButtonCellから派生し、派生クラスに新しいプロパティを追加する場合は、Clone() メソッドをオーバーライドして、複製操作中に新しいプロパティをコピーしてください。 基底クラスのプロパティが新しいセルにコピーされるように、基底クラスの Clone() メソッドも呼び出す必要があります。
コンストラクター
| 名前 | 説明 |
|---|---|
| DataGridViewButtonCell() |
DataGridViewButtonCell クラスの新しいインスタンスを初期化します。 |
プロパティ
| 名前 | 説明 |
|---|---|
| AccessibilityObject |
DataGridViewCell.DataGridViewCellAccessibleObjectに割り当てられたDataGridViewCellを取得します。 (継承元 DataGridViewCell) |
| ColumnIndex |
このセルの列インデックスを取得します。 (継承元 DataGridViewCell) |
| ContentBounds |
セルのコンテンツ領域を囲む外接する四角形を取得します。 (継承元 DataGridViewCell) |
| ContextMenuStrip |
セルに関連付けられているショートカット メニューを取得または設定します。 (継承元 DataGridViewCell) |
| DataGridView |
この要素に関連付けられている DataGridView コントロールを取得します。 (継承元 DataGridViewElement) |
| DefaultNewRowValue |
新しいレコードの行のセルの既定値を取得します。 (継承元 DataGridViewCell) |
| Displayed |
セルが現在画面上に表示されているかどうかを示す値を取得します。 (継承元 DataGridViewCell) |
| EditedFormattedValue |
セルが編集モードであり、値がコミットされていないかどうかに関係なく、セルの現在の書式設定された値を取得します。 (継承元 DataGridViewCell) |
| EditType |
セルのホステッド編集コントロールの型を取得します。 |
| ErrorIconBounds |
セルのエラー アイコンの境界を取得します。 (継承元 DataGridViewCell) |
| ErrorText |
セルに関連付けられているエラー条件を説明するテキストを取得または設定します。 (継承元 DataGridViewCell) |
| FlatStyle |
ボタンの外観を決定するスタイルを取得または設定します。 |
| FormattedValue |
表示用に書式設定されたセルの値を取得します。 (継承元 DataGridViewCell) |
| FormattedValueType |
セルに関連付けられている書式設定された値の型を取得します。 |
| Frozen |
セルが固定されているかどうかを示す値を取得します。 (継承元 DataGridViewCell) |
| HasStyle |
Style プロパティが設定されているかどうかを示す値を取得します。 (継承元 DataGridViewCell) |
| InheritedState |
行と列の状態から継承されたセルの現在の状態を取得します。 (継承元 DataGridViewCell) |
| InheritedStyle |
現在セルに適用されているスタイルを取得します。 (継承元 DataGridViewCell) |
| IsInEditMode |
このセルが現在編集されているかどうかを示す値を取得します。 (継承元 DataGridViewCell) |
| OwningColumn |
このセルを含む列を取得します。 (継承元 DataGridViewCell) |
| OwningRow |
このセルを含む行を取得します。 (継承元 DataGridViewCell) |
| PreferredSize |
セルが収まる四角形領域のサイズ (ピクセル単位) を取得します。 (継承元 DataGridViewCell) |
| ReadOnly |
セルのデータを編集できるかどうかを示す値を取得または設定します。 (継承元 DataGridViewCell) |
| Resizable |
セルのサイズを変更できるかどうかを示す値を取得します。 (継承元 DataGridViewCell) |
| RowIndex |
セルの親行のインデックスを取得します。 (継承元 DataGridViewCell) |
| Selected |
セルが選択されているかどうかを示す値を取得または設定します。 (継承元 DataGridViewCell) |
| Size |
セルのサイズを取得します。 (継承元 DataGridViewCell) |
| State |
要素のユーザー インターフェイス (UI) 状態を取得します。 (継承元 DataGridViewElement) |
| Style |
セルのスタイルを取得または設定します。 (継承元 DataGridViewCell) |
| Tag |
セルに関する補足データを含むオブジェクトを取得または設定します。 (継承元 DataGridViewCell) |
| ToolTipText |
このセルに関連付けられているツールヒント テキストを取得または設定します。 (継承元 DataGridViewCell) |
| UseColumnTextForButtonValue |
所有する列のテキストがセルによって表示されるボタンに表示されるかどうかを示す値を取得または設定します。 |
| Value |
このセルに関連付けられている値を取得または設定します。 (継承元 DataGridViewCell) |
| ValueType |
セル内の値のデータ型を取得または設定します。 |
| Visible |
セルが非表示になっている行または列のどちらにあるかを示す値を取得します。 (継承元 DataGridViewCell) |