Alice
Alice

Reputation: 633

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.

Upvotes: 10

Views: 11183

Answers (4)

HatSoft
HatSoft

Reputation: 11201

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................
    }
}

Upvotes: 1

Vale
Vale

Reputation: 3298

private void dgv_CellDoubleClick(object sender, DataGridViewCellEventArgs e) {
            if (e.RowIndex != -1) {
                //do work
            }
        }

Upvotes: 23

mgnoonan
mgnoonan

Reputation: 7200

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

Upvotes: 3

yogi
yogi

Reputation: 19601

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

Upvotes: 2

Related Questions