Click here to Skip to main content
16,015,393 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
when clicking on columnheader for sorting the grid also the clickevent is raised.
within the clickevent I do several things with the current row (mark/unmark) -
so when I want to sort the grid, my mark will also be set - how can I cancel this ?
Is it possible to readout wether I clicked within the grid or on columnheader ?

private void dataGridView1_Click(object sender, EventArgs e)
{
    if (dataGridView1.CurrentRow is null) return;
    Log("dataGridView1_Click" + dataGridView1.CurrentRow.Index.ToString());
    bool myBool;
    myBool = !(bool)dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells["X"].Value; // invertieren !
    dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells["X"].Value = myBool;
    myColumnDict[dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells["COLUMN_NAME"].Value.ToString()] = myBool;
}


What I have tried:

log the sequence of the raised event when clicking column-header:
1. datagrid.click
2. datagrid.sorted
Posted
Comments
0x01AA 21hrs ago    
Use dataGridView1_CellClick. There you can check the row index, which is -1 in case a header cell is clicked.

1 solution

I would use a second event for "CellClick" which should not be fired on a Header Click.

Thus you can combine your conditions dependent on two different events.
 
Share this answer
 
Comments
0x01AA 21hrs ago    
CellClick will be fired also when you click into header. But especally in CellClick is, you get the row index which is -1 in case a header cell has been clicked ;)
+5
KANNE1466 20hrs ago    
great - that works:
moved action to event "CellClick" and there I can examine the clicked row
Jo_vb.net 20hrs ago    
Thanks for improving my idea!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900