DataGridView.CellContentClick イベント
アセンブリ: System.Windows.Forms (system.windows.forms.dll 内)

Dim instance As DataGridView Dim handler As DataGridViewCellEventHandler AddHandler instance.CellContentClick, handler
public: event DataGridViewCellEventHandler^ CellContentClick { void add (DataGridViewCellEventHandler^ value); void remove (DataGridViewCellEventHandler^ value); }

このイベントは、セルの内容がクリックされたときに発生します。ボタン セルまたはチェック ボックス セルにフォーカスがある状態で Space キーを押し、離したときにも発生します。また、これらのセル タイプでは、Space キーを押しながらセルの内容をクリックした場合は、このイベントが 2 回発生します。
DataGridViewButtonCell のボタン クリック、または DataGridViewLinkCell のリンク クリックを検出するには、このイベントを使用します。
DataGridViewCheckBoxCell をクリックすると、このイベントは、チェック ボックスが値を変更する前に発生します。したがって、現在の値に基づいて予期される値を計算する必要がない場合、通常はこのイベントの代わりに DataGridView.CellValueChanged イベントを処理します。このイベントは、ユーザー指定の値がコミットされたとき (通常はフォーカスがセルを離れたとき) にだけ発生するため、DataGridView.CurrentCellDirtyStateChanged イベントも処理する必要があります。そのハンドラでは、現在のセルがチェック ボックス セルの場合、DataGridView.CommitEdit メソッドを呼び出して、Commit 値を渡します。

このイベントのハンドラを提供して、クリックされたセルがリンク セルまたはボタン セルかどうかを判断し、結果として対応する操作を実行するコード例を次に示します。
Private Sub DataGridView1_CellContentClick(ByVal sender As Object, _ ByVal e As DataGridViewCellEventArgs) _ Handles DataGridView1.CellContentClick If IsANonHeaderLinkCell(e) Then MoveToLinked(e) ElseIf IsANonHeaderButtonCell(e) Then PopulateSales(e) End If End Sub Private Sub MoveToLinked(ByVal e As DataGridViewCellEventArgs) Dim employeeId As String Dim value As Object = DataGridView1.Rows(e.RowIndex). _ Cells(e.ColumnIndex).Value If value.GetType Is GetType(DBNull) Then Return employeeId = CType(value, String) Dim boss As DataGridViewCell = _ RetrieveSuperiorsLastNameCell(employeeId) If Not boss Is Nothing Then DataGridView1.CurrentCell = boss End If End Sub Private Function IsANonHeaderLinkCell(ByVal cellEvent As _ DataGridViewCellEventArgs) As Boolean If TypeOf DataGridView1.Columns(cellEvent.ColumnIndex) _ Is DataGridViewLinkColumn _ AndAlso Not cellEvent.RowIndex = -1 Then _ Return True Else Return False End Function Private Function IsANonHeaderButtonCell(ByVal cellEvent As _ DataGridViewCellEventArgs) As Boolean If TypeOf DataGridView1.Columns(cellEvent.ColumnIndex) _ Is DataGridViewButtonColumn _ AndAlso Not cellEvent.RowIndex = -1 Then _ Return True Else Return (False) End Function Private Function RetrieveSuperiorsLastNameCell( _ ByVal employeeId As String) As DataGridViewCell For Each row As DataGridViewRow In DataGridView1.Rows If row.IsNewRow Then Return Nothing If row.Cells(ColumnName.EmployeeId.ToString()). _ Value.ToString().Equals(employeeId) Then Return row.Cells(ColumnName.LastName.ToString()) End If Next Return Nothing End Function
private void DataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e) { if (IsANonHeaderLinkCell(e)) { MoveToLinked(e); } else if (IsANonHeaderButtonCell(e)) { PopulateSales(e); } } private void MoveToLinked(DataGridViewCellEventArgs e) { string employeeId; object value = DataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value; if (value is DBNull) { return; } employeeId = value.ToString(); DataGridViewCell boss = RetrieveSuperiorsLastNameCell(employeeId); if (boss != null) { DataGridView1.CurrentCell = boss; } } private bool IsANonHeaderLinkCell(DataGridViewCellEventArgs cellEvent) { if (DataGridView1.Columns[cellEvent.ColumnIndex] is DataGridViewLinkColumn && cellEvent.RowIndex != -1) { return true; } else { return false; } } private bool IsANonHeaderButtonCell(DataGridViewCellEventArgs cellEvent) { if (DataGridView1.Columns[cellEvent.ColumnIndex] is DataGridViewButtonColumn && cellEvent.RowIndex != -1) { return true; } else { return (false); } } private DataGridViewCell RetrieveSuperiorsLastNameCell(string employeeId) { foreach (DataGridViewRow row in DataGridView1.Rows) { if (row.IsNewRow) { return null; } if (row.Cells[ColumnName.EmployeeId.ToString()].Value.ToString().Equals(employeeId)) { return row.Cells[ColumnName.LastName.ToString()]; } } return null; }
private: void DataGridView1_CellContentClick(Object^ /*sender*/, DataGridViewCellEventArgs^ e) { if (IsANonHeaderLinkCell(e)) { MoveToLinked(e); } else if (IsANonHeaderButtonCell(e)) { PopulateSales(e); } } private: void MoveToLinked(DataGridViewCellEventArgs^ e) { String^ employeeId; Object^ value = DataGridView1->Rows[e->RowIndex]->Cells[e->ColumnIndex]->Value; if (dynamic_cast<DBNull^>(value) != nullptr) { return; } employeeId = value->ToString(); DataGridViewCell^ boss = RetrieveSuperiorsLastNameCell(employeeId); if (boss != nullptr) { DataGridView1->CurrentCell = boss; } } private: bool IsANonHeaderLinkCell(DataGridViewCellEventArgs^ cellEvent) { if (dynamic_cast<DataGridViewLinkColumn^>(DataGridView1->Columns[cellEvent->ColumnIndex]) != nullptr && cellEvent->RowIndex != -1) { return true; } else { return false; } } private: bool IsANonHeaderButtonCell(DataGridViewCellEventArgs^ cellEvent) { if (dynamic_cast<DataGridViewButtonColumn^>(DataGridView1->Columns[cellEvent->ColumnIndex]) != nullptr && cellEvent->RowIndex != -1) { return true; } else { return (false); } } private: DataGridViewCell^ RetrieveSuperiorsLastNameCell(String^ employeeId) { for each (DataGridViewRow^ row in DataGridView1->Rows) { if (row->IsNewRow) { return nullptr; } if (row->Cells[ColumnName::EmployeeID.ToString()]->Value->ToString()->Equals(employeeId)) { return row->Cells[ColumnName::LastName.ToString()]; } } return nullptr; }

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


DataGridView クラス
DataGridView メンバ
System.Windows.Forms 名前空間
その他の技術情報
DataGridView コントロール (Windows フォーム)
Weblioに収録されているすべての辞書からDataGridView.CellContentClick イベントを検索する場合は、下記のリンクをクリックしてください。

- DataGridView.CellContentClick イベントのページへのリンク