DataGridView.CellContentClick 事件
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
按一下儲存格內的內容時發生。
public:
event System::Windows::Forms::DataGridViewCellEventHandler ^ CellContentClick;
public event System.Windows.Forms.DataGridViewCellEventHandler CellContentClick;
public event System.Windows.Forms.DataGridViewCellEventHandler? CellContentClick;
member this.CellContentClick : System.Windows.Forms.DataGridViewCellEventHandler
Public Custom Event CellContentClick As DataGridViewCellEventHandler
事件類型
範例
下列程式碼範例會提供這個事件的處理常式,判斷按一下的資料格是連結儲存格還是按鈕儲存格,並執行對應的動作作為結果。 此範例是類別概觀主題中較大範例的 DataGridViewComboBoxColumn 一部分。
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;
}
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 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
備註
按一下儲存格內容時,就會發生此事件。 當使用者按下並放開 SPACEBAR 時,按鈕儲存格或核取方塊儲存格有焦點時,也會發生兩次這些儲存格類型,如果按下 SPACEBAR 時按一下儲存格內容,也會發生兩次。
使用此事件來偵測 的按鈕點擊, DataGridViewButtonCell 或 的連結點選 DataGridViewLinkCell 。
對於 中的 DataGridViewCheckBoxCell 按一下 ,此事件會在核取方塊變更值之前發生,因此如果您不想根據目前值計算預期的值,您通常會改為處理 DataGridView.CellValueChanged 事件。 因為只有在認可使用者指定的值時才會發生該事件,這通常會發生在焦點離開儲存格時,您也必須處理 DataGridView.CurrentCellDirtyStateChanged 事件。 在該處理常式中,如果目前的儲存格是核取方塊儲存格,請呼叫 DataGridView.CommitEdit 方法並傳入 Commit 值。
如需如何處理事件的詳細資訊,請參閱 處理和引發事件。