views:

204

answers:

1

This Code triggers selection change event twice. how can I prevent it ? Currently i m using a flag or focused property to prevent this. But what is the actual way ?

I am using it on winfoms

EDIT

My Mistake in writing Question, here is the correct code that i wanted to ask

private void frmGuestInfo_Load(object sender, EventArgs e)
{
this.dgvGuestInfo.SelectionChanged -= new System.EventHandler(this.dgvGuestInfo_SelectionChanged);
dgvGuestInfo.DataSource=dsFillControls.Tables["tblName"];
this.dgvGuestInfo.SelectionChanged += new System.EventHandler(this.dgvGuestInfo_SelectionChanged);
}

private void dgvGuestInfo_SelectionChanged(object sender, EventArgs e)
{
//this function is raised twice, i was expecting that this will not be raised 
}
A: 
SLaks
@Slaks: Why this happens, how could it be avoided. I even canceled event at the moment i am setting datasouce and rebinding the event after that. Then why it still raises event
Shantanu Gupta
Why are you setting `DataSource` twice? Put a breakpoint in the handler, disable `Just My Code`, and look at the call stack.
SLaks
Also, make sure you aren't adding the handler twice. Right-click on `dgvGuestInfo_SelectionChanged` and click Find All References.
SLaks