DataGridView's SelectionChange event firing twice on DataBinding even after removing event binding
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, Even开发者_开发百科tArgs 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.
精彩评论