Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run a code only if a Cell, not a Header, in DataGridView is doubleClicked?

private void dgv_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
foreach (DataGridViewRow r in dgv.Rows) r.Visible = false;
}

This code works, but also works if ColumnHeaders (not only cells) is doubleClicked ?
I want to run it only if a cell is doubleClicked.
CellDoubleClick should mean CellDoubleClick and not HeaderDoubleClick.

like image 890
Alice Avatar asked Jul 02 '12 12:07

Alice


4 Answers

private void dgv_CellDoubleClick(object sender, DataGridViewCellEventArgs e) {
            if (e.RowIndex != -1) {
                //do work
            }
        }
like image 59
Vale Avatar answered Oct 08 '22 19:10

Vale


You could check if e.RowIndex is -1, which means the event happened on a header row.

like image 20
mgnoonan Avatar answered Oct 08 '22 18:10

mgnoonan


You can use DataGridViewCellEventArgs.RowIndex to check if the header is clicked or any cell from the rows is clicked.

like image 2
yogi Avatar answered Oct 08 '22 19:10

yogi


Not the cleanest way to do but you can achieve it like this

private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
    if (((System.Windows.Forms.DataGridView)(sender)).CurrentCell != null)
    {
       //Do what you want here................
    }
}
like image 1
HatSoft Avatar answered Oct 08 '22 18:10

HatSoft