This Code triggers selection change event twice. how can I prevent it ? Currently im 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
}
The event will fire each time you set the
DataSource
property.
You should only set
DataSource
once.
You might be adding the same event handler twice.
Right-click on dgvGuestInfo_SelectionChanged
and click Find All References.
Also, check the call stack in the event handler.
I have your very same problem: sometimes deregistering from SelectionChanged works, sometimes not.
And I'm unregistering/reregistering in a try/finally construct:
this.SelectionChanged -= ManageSelectionChanged;
try
{
// code that could fire this.SelectionChanged
}
finally
{
this.SelectionChanged += ManageSelectionChanged;
}
I've choosen to use a private flag too, but... I'm still curious.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.