tags:

views:

201

answers:

1

FYI, I looked at existing postings and I did not find exactly what I needed.

I am using Visual Studio 2008 to write a C# Windows Forms program. I have a DataGridView object that displays data from a table. When a user clicks on a cell I want to be able to grab the contents of that cell.

How do get an action to take place whenever there is a click on any cell in the datagridview. If the user clicks on the same cell 5 times in a row, I want the action to happen five times.

I am not even sure what the name of the event handler would be.

I tried the following, but it didn't work.

This was the code on the FormName.cs file:

private void DATASOURCEDataGridView_CellContentClick(object sender, 
           DataGridViewCellEventArgs e)
{
    MessageBox.Show("clicked");
}

This was the code on the FormName.Designer.cs File:

this.DATASOURCEDataGridView.CellContentClick += 
    new System.Windows.Forms.DataGridViewCellEventHandler(
        this.DATASOURCEDataGridView_CellContentClick);

Thanks for the help!

A: 

Use the CellClickedEvent:

 private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
          MessageBox.Show(dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString());
        }
AlexDrenea
Thanks Alex, this was exactly what I needed. I needed to use CellClick instead of CellContentClick. Problem solved! Much appreciated!!!
JK
FYI to anyone else using this code. It still needs one modification. If the user clicks on the column header or row header there is a column or row out of range error. You just need to add some additional logic to handle that scenario and it works great.
JK