0

SelectionChanged methods are triggered when the selection is changed by program. So, for example, calling dataGridView.ClearSelection() or dataGridView.Rows[0].Selected = true would call the method

private void dataGridView_SelectionChanged(object sender, EventArgs e)
{
}

Is it possible to execute code only when the user changed the selection, e.g. by selecting a row/cell with the mouse or keyboard?

jacobz
  • 3,191
  • 12
  • 37
  • 61
  • There's no good way to do it, refer to this: http://stackoverflow.com/questions/650784/determine-if-changed-event-occurred-from-user-input-or-not – Ben Black Jun 09 '14 at 14:12
  • As far as i know: no. But you can add some logic to this method and write your code in it after if statement. I think you should be able to get some information about the event. Hope this will be helpful. – sdrzymala Jun 09 '14 at 14:15

1 Answers1

0

You will have to code this in

private bool _programmaticChange;

private void SomeMethod()
{
    _programmaticChange = true;
    dataGridView.ClearSelection();
    _programmaticChange = false;
}


private void dataGridView_SelectionChanged(object sender, EventArgs e)
{
    if (_programmaticChange) return;
    // some code
}

this will make it run only on user actions

T.S.
  • 18,195
  • 11
  • 58
  • 78