datagridview cell click event c# code example

Example 1: c# datagridview cell click event

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
    if (dataGridView1.CurrentCell.ColumnIndex.Equals(3) && e.RowIndex != -1){
        if (dataGridView1.CurrentCell != null && dataGridView1.CurrentCell.Value != null)
            MessageBox.Show(dataGridView1.CurrentCell.Value.ToString());   
}

Example 2: datagridview mouse click event c#

public event DataGridViewCellMouseEventHandler CellMouseClick;

    private void DataGridView1_CellMouseClick(Object sender, DataGridViewCellMouseEventArgs e)
    {
        MessageBox.Show("Mouse clicked in the datagridview!");
    }

Example 3: datagridview mouse click event c#

DataGridView1.CellMouseClick += DataGridView1_CellMouseClick;