英語で読む

次の方法で共有


DataGridView.CellContentClick イベント

定義

セル内の内容がクリックされたときに発生します。

public event System.Windows.Forms.DataGridViewCellEventHandler CellContentClick;
public event System.Windows.Forms.DataGridViewCellEventHandler? CellContentClick;

イベントの種類

次のコード例では、クリックしたセルがリンク セルかボタン セルかを決定し、結果として対応するアクションを実行する、このイベントのハンドラーを提供します。 この例は、クラスの概要に関するトピックで使用できる大きな例の 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 (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;
}

注釈

このイベントは、セルの内容がクリックされたときに発生します。 また、ユーザーが SPACE キーを押して離すと、ボタン セルまたは チェック ボックス セルにフォーカスがあり、SPACE キーを押しながらセルの内容がクリックされると、これらのセルの種類に対して 2 回発生します。

このイベントを使用して、 または のリンククリック DataGridViewButtonCell のボタンクリックを DataGridViewLinkCell検出します。

内のDataGridViewCheckBoxCellクリックの場合、このイベントは、チェック ボックスの値が変更される前に発生するため、現在の値に基づいて期待値を計算しない場合は、通常、代わりにイベントを処理しますDataGridView.CellValueChanged。 このイベントは、ユーザー指定の値がコミットされたときにのみ発生するため、通常はフォーカスがセルから離れたときに発生します。イベントも処理する DataGridView.CurrentCellDirtyStateChanged 必要があります。 そのハンドラーで、現在のセルがチェックボックス セルの場合は、 メソッドをDataGridView.CommitEdit呼び出して 値をCommit渡します。

イベントを処理する方法の詳細については、次を参照してください。処理とイベントの発生します。

適用対象

製品 バージョン
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

こちらもご覧ください