datagridview cell click event
Asked Answered
N

6

9

I have an event for a cell click in a datagrid view to display the data in the clicked cell in a message box. I have it set to where it only works for a certain column and only if there is data in the cell

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

however, whenever i click any of the column headers, a blank messagebox shows up. I cant figure out why, any tips?

Niue answered 6/10, 2012 at 17:22 Comment(0)
Q
28

You will also need to check the cell clicked is not the column header cell. Like this:

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());   
}
Quiroz answered 6/10, 2012 at 18:18 Comment(1)
Just notice that you should check ` dataGridView1.CurrentCell != null ` before the first condition ...Cusp
V
2

Check that CurrentCell.RowIndex isn't the header row index.

Vantassel answered 6/10, 2012 at 17:33 Comment(0)
D
2
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{    
    if (e.RowIndex == -1) return; //check if row index is not selected
        if (dataGridView1.CurrentCell.ColumnIndex.Equals(3))
            if (dataGridView1.CurrentCell != null && dataGridView1.CurrentCell.Value != null)
                MessageBox.Show(dataGridView1.CurrentCell.Value.ToString());
}
Donnelldonnelly answered 6/10, 2012 at 21:25 Comment(0)
H
1

The accepted solution throws an "object not set to an instance of an object" exception as null reference checking MUST happen before checking the actual value of a variable.

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{    
    if (dataGridView1.CurrentCell == null ||
        dataGridView1.CurrentCell.Value == null ||
        e.RowIndex == -1) return;
    if (dataGridView1.CurrentCell.ColumnIndex.Equals(3))
        MessageBox.Show(dataGridView1.CurrentCell.Value.ToString());
}
Honor answered 19/2, 2017 at 18:12 Comment(0)
U
0

try this

        if(dataGridView1.Rows.Count > 0)
            if (dataGridView1.CurrentCell.ColumnIndex == 3)
                MessageBox.Show(dataGridView1.CurrentCell.Value.ToString());
Uncaused answered 16/5, 2017 at 3:17 Comment(0)
A
0
  private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dataGridView1.CurrentCell.ColumnIndex.Equals(0))
            {
                foreach (DataGridViewRow row in dataGridView1.SelectedRows)
                {
                 enter code here
                }
            }
         }
Ani answered 7/12, 2022 at 12:57 Comment(1)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Implicatory

© 2022 - 2024 — McMap. All rights reserved.