Click here to Skip to main content
15,886,835 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I've some problem using datagridview..The selectionchange event of Datagridview triggers several times,
Due to these triggers the code written in function executes more then one time .
How can I prevent it ? Is there any other similar event that i can use ?
(In my datagridview I have the SelectionMode property set to FullRowSelect)

for example when the selection is changed ,selection is clear (by calling clearselection ),
when RowCount property of the DataGridView is increase or decrease.

VB
dgvCaseLst1.RowCount = 0
dgvCaseLst1.RowCount = 22
dgvCaseLst1.ClearSelection()
dgvCaseLst1.Rows(Pi_CaseIndex).Selected = True 
 
 Private Sub DataGridView1_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles dgvCaseLst1.SelectionChanged
 'It triggers 4 times   
 MessageBox.Show("selection changed trigger")
 End Sub
Posted
Comments
BulletVictim 7-Aug-13 4:58am    
Quick but dirty way. I would say declare an int with value of lets say 1, in DataGridView1_SelectionChanged run an if int = 1 then have the code execute, at the end set the int to 0.
So it should be something like this:
Private Sub DataGridView1_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs)
if int i = 1 then
Handles dgvCaseLst1.SelectionChanged
'It triggers 4 times
MessageBox.Show("selection changed trigger")
int i = 0
end if
End Sub
muneebalikiyani 12-Aug-13 0:27am    
i am using this approach but looking for a better one. Thanks
stixoffire 16-Oct-16 22:04pm    
I know this is late but for others that might be reading.
dgvCaseLst1.ClearSelection() - The selection Changed event will Fire!
dgvCaseLst1.Rows(Pi_CaseIndex).Selected = True , The selection Changed the Event will fire!
This code inside the event handler
dgvCaseLst1.Rows(Pi_CaseIndex).Selected = True
Will not cause a SelectionChanged Event because the selection did not change from the line that set this to begin with. Of course this depends on the order of his calling methods. Do not trigger the same event with in the handler - this is a no no. There is a lot wrong with the code above but that is why he has the event firing like crazy.

1 solution

Why are you putting an event handler inside an event handler?

Just put in there what you want it to do.

VB
Private Sub DataGridView1_SelectionChanged(ByVal sender As Object, ByVal e As 
'dgvCaseLst1.RowCount = 0
'dgvCaseLst1.RowCount = 22
'dgvCaseLst1.ClearSelection()
dgvCaseLst1.Rows(Pi_CaseIndex).Selected = True
MessageBox.Show("selection changed trigger")
End Sub


you are trying to do to many things
first you are setting the row count to 0
then you are setting it to 22
then you are clearing the selections.
I'm not even sure what the next one is supposed to do.

You have to make up your mind what you want it to do.

The way it was it was doing all of 4 them thats why you got 4 triggers.

If you need it to do different things use a if /else or a case statement.

for me the selected change is for doing something with the data in the row/cell that was selected.
But here you appear to be trying to change another datagrid view when the change event is fired in the first one ???
 
Share this answer
 
Comments
muneebalikiyani 12-Aug-13 3:59am    
i didn't put event handler inside event handle. The statements mentioned above use to trigger the event handler.
I am using only one Data grid view and while performing the above events the selection changed event handler triggered , Want to minimize that .
Please guide Thanks
ledtech3 12-Aug-13 10:05am    
Sorry the line wrapped and it just looked that way at first.
Best I can tell when the event does fire it is running all 4 events you have above it.
Not sure why you are doing it that way either.

What are you wanting it to do?

They have a built in row selection.
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.selectionmode.aspx

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