更新:2007 年 11 月
在单元格中的内容被单击时发生。
命名空间: System.Windows.Forms
程序集: System.Windows.Forms(在 System.Windows.Forms.dll 中)

Visual Basic(声明)
|
Public Event CellContentClick As DataGridViewCellEventHandler |
Visual Basic (用法)
|
Dim instance As DataGridView Dim handler As DataGridViewCellEventHandler AddHandler instance.CellContentClick, handler |
C#
|
public event DataGridViewCellEventHandler CellContentClick
|
Visual C++
|
public: event DataGridViewCellEventHandler^ CellContentClick { void add (DataGridViewCellEventHandler^ value); void remove (DataGridViewCellEventHandler^ value); } |
J#
|
/** @event */ public void add_CellContentClick (DataGridViewCellEventHandler value) /** @event */ public void remove_CellContentClick (DataGridViewCellEventHandler value) |
JScript
|
JScript 不支持事件。 |

在单元格内容被单击时发生此事件。当按钮单元格或复选框单元格具有焦点时,此事件还在用户按下并松开空格键时发生;如果在按下空格键的同时,该单元格的内容被单击,此事件将针对这些类型的单元格发生两次。
使用此事件可以检测按钮单击(对于 DataGridViewButtonCell)或链接单击(对于 DataGridViewLinkCell)。
单击 DataGridViewCheckBoxCell 时,此事件在复选框更改值之前发生,因此,如果您不希望基于当前值来计算期望值,通常应转而处理 DataGridView..::.CellValueChanged 事件。由于事件仅在提交用户指定的值时发生,而用户指定的值又通常是在焦点离开单元格时提交,因此,您还必须处理 DataGridView..::.CurrentCellDirtyStateChanged 事件。在该处理程序中,如果当前单元格是复选框单元格,请调用 DataGridView..::.CommitEdit 方法并传入 Commit 值。
有关处理事件的更多信息,请参见使用事件。

下面的代码示例提供此事件的处理程序,该处理程序确定被单击的单元格是链接单元格还是按钮单元格,并据此执行相应的操作。此示例摘自 DataGridViewComboBoxColumn 类概述主题中提供的一个更大的示例。
Visual Basic
|
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 boss IsNot 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 |
C#
|
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; } |
Visual C++
|
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; } |