Link to home
Start Free TrialLog in
Avatar of emi_sastra
emi_sastra

asked on

Prevent call a sub/function several times when data bind to gridview

Hi All,

I have a header and detail gridview.

Once I bind data to header gridview.
The sub to fill detail gridview called several times.

The event trigger is at

  Private Sub dgvHeader_SelectionChanged(sender As Object, e As EventArgs) Handles dgvHeader.SelectionChanged
        Me.Open_Data_Detail()
        Screen_Function.Focusing_Control(dgvHeader)
    End Sub

How could I prevent this ?

Thank you.
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

How could I prevent this ?
you may try to check if the page is posted back, and then do your stuffs accordingly, like:

Private Sub dgvHeader_SelectionChanged(sender As Object, e As EventArgs) Handles dgvHeader.SelectionChanged
        If Not Page.IsPostBack Then
             ' your original stuffs to do
       Else
             ' if page is posted back
       End If
    End Sub

Open in new window

Avatar of emi_sastra
emi_sastra

ASKER

Hi Ryan,

This is windows app ?

Thank you.
if this is Windows App, firstly, you can initial a variable with a default value.

once your stuff is executed, try to set it with another value, so that when that event is triggered again, it won't execute the same codes again, like:

Dim isFirstTime As Boolean = True

Private Sub dgvHeader_SelectionChanged(sender As Object, e As EventArgs) Handles dgvHeader.SelectionChanged
        If isFirstTime Then
             ' your original stuffs to do
            isFirstTime  = false
       Else
             ' if it's subsequent events
       End If
    End Sub

Open in new window

I have done this before at form load.


sub Form_Load
blnload = true

blnload = false
 
End sub

After the false, the dgvHeader_SelectionChanged still trigger several times.

Thank you.
so you only want to do it once for codes below OR you want to do it after first time your header is changed?

Me.Open_Data_Detail()
        Screen_Function.Focusing_Control(dgvHeader)

Open in new window

-so you only want to do it once for codes below OR you want to do it after first time your header is changed?
Yes.

Thank you.
-so you only want to do it once for codes below OR you want to do it after first time your header is changed?
Just once.

Thank you.
Just once when refresh header.

Thank you.
ASKER CERTIFIED SOLUTION
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi Ryan,

When blnload will bet set to true again in order to open detail ?

Thank you.
If I moving around header grid using mouse or updown key ?

Thank you.
You can set master/details view using bindingsources via code or at design time - bind master grid to parent table (as datasource) and details grid to same datasource and set datamember to child table
Hi Ark,

What is the advantage/disadvantage of using binding and not ?

Thank you.
Using binding :

What if I show I pop up form to add/update/delete data, how to refresh it ?

Thank you.
When blnload will bet set to true again in order to open detail ?
if your datagrid is in a form and you closed it, the next time when you re-open the form, the initial value is always set as true. for subsequent refreshed, it should be remained as false.
Private Sub dgvHeader_SelectionChanged(sender As Object, e As EventArgs) Handles dgvHeader.SelectionChanged
        If blnload Then
            Me.Open_Data_Detail()
             Screen_Function.Focusing_Control(dgvHeader)
            blnload = false
       End If
    End Sub

It is always false. Means  Me.Open_Data_Detail() never get called.

Thank you.
did you add this line on top of your codes?

Dim blnload As Boolean = True

Open in new window

- did you add this line on top of your codes?
Sure, then when it is set to true again ?

this is not local variable.
 If blnload Then
endif

Thank you.
this is not local variable.
do you have a sample to upload here for better illustration?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi All,

Thank you very much for your help.