Solved

how can I refresh my datagridview without using a refresh button?

Posted on 2010-08-26
15
700 Views
Last Modified: 2013-11-27
I have two forms one of them has a datagridview and the other is used to update the record the user clicks on the grid.  When the user closes the form use to update the record, the form closes and then the user needs to click on refresh before the updated record shows in the datagridview.
What event shoud I use to automate the refreshing of my data in a datagridview?.  Right now, I have the code in a refresh button in the form containing the datagridvie which works fine, but I would like the update of the datagridview to occur automatically when the user closes the form used to update the record.  The code is fine since it is updating the datagridview but  WHERE DO I PUT MY CODE SO I Can see the data in my datagridview change without having to click any buttons ?  Thank you,
Gloria
0
Comment
Question by:gloriagalvez
  • 6
  • 4
  • 4
  • +1
15 Comments
 
LVL 17

Expert Comment

by:Zhaolai
Comment Utility
Change the Refresh button click event to a sub.
 Call the sub right after your update form's ShowDialog.
0
 
LVL 16

Expert Comment

by:13598
Comment Utility
You could execute the code in your formclosed event of the form used to update the record.
And if you still need to have the refresh button for some other reason you could just in your formclosed event do a refreshbutton.performclick.
That will programmatically do the same as if the user had clicked on the button.
 
0
 
LVL 3

Expert Comment

by:raaziq
Comment Utility
var returnValue = window.showModalDialog("path");
if (returnValue == "1")            
                document.getElementById('<%= btnRefresh.ClientID %>').click();

make btnRefresh (either button or linkbutton) attribute as display none.

------------------------------------------------------------------

inside the popup
on window close use this,

 window.returnValue = "1"
 window.close();
0
 

Author Comment

by:gloriagalvez
Comment Utility
I tried all of the suggestions above and they did not work for me.  I was getting the data in duplicate.  I got it to work with the paint event.  would I have any problems in the future using this event?  It seems to work fine.
0
 
LVL 16

Expert Comment

by:13598
Comment Utility
The paint event gets triggered many times by many things. Performance wise is not recommended to put your data refresh in that event. You will be refreshing your data 'a lot'.
Try using your validated or databindingcomplete events.
I don't have enough information to be able to suggest appropriately.
0
 
LVL 17

Expert Comment

by:Zhaolai
Comment Utility
Combine @13598's with my first comment. Add the following line:

        btnRefresh.PerformClick()

after  your update form's ShowDialog() in your main form.


0
 

Author Comment

by:gloriagalvez
Comment Utility
To use 13598's I would need to create global variables which i don't really like.  Right now, the command, dataadapter, datatable and connection are shared in the same form (the one that shows all records).  If I want to put the refresh code there I would need to have access to those variables:  Please look at the code of my paint event as it is now and working.

Private Sub frmShowAllProspects_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
        Me.mydgCustomer.DataSource = Nothing
        da = New SqlDataAdapter(sqlCommand)
        dt.Clear()
        Try
            da.Fill(dt)
            Me.mydgCustomer.DataSource = dt

        Catch ex As Exception
            MsgBox(ex.Message & " " & ex.Source)
        End Try

    End Sub
Would it be another way to accomplish this?  Thank you.
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 17

Expert Comment

by:Zhaolai
Comment Utility
In frmShowAllProspects, you must have an event to open the second form to update the data. In that event, you must have code like this:
    Form2.ShowDialog()

Now move all the code in the Paint event to after above line, like this:

        Form2.ShowDialog()
         Me.mydgCustomer.DataSource = Nothing
        da = New SqlDataAdapter(sqlCommand)
        dt.Clear()
        Try
            da.Fill(dt)
            Me.mydgCustomer.DataSource = dt

        Catch ex As Exception
            MsgBox(ex.Message & " " & ex.Source)
        End Try
End Sub

0
 

Author Comment

by:gloriagalvez
Comment Utility
The update of my datagridview then only happens when i open the form again.
0
 

Author Comment

by:gloriagalvez
Comment Utility
This is where you suggested I added the code:  
Private Sub mydgCustomer_MouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles mydgCustomer.MouseDoubleClick
        Dim clickedCell As DataGridViewCell
        'Dim clsProspectID As New clsProspect

        Dim hit As DataGridView.HitTestInfo = _
            mydgCustomer.HitTest(e.X, e.Y)
        If hit.Type = DataGridViewHitTestType.Cell Then
            clickedCell = _
                mydgCustomer.Rows(hit.RowIndex).Cells(hit.ColumnIndex)
        End If
        Try

            clsProspect.ProspectID = CInt(mydgCustomer.Item(9, rowIndex:=hit.RowIndex).Value)
            'intProspectID = CInt(hit.RowIndex.ToString) '''THIS DISPLAYS THE ROW NUMBER
            ''''Need to set the clsProspect class = nothing when finished after the detail form''''
            intProspectID = clsProspect.ProspectID

            frmDetail.Show()
            Me.mydgCustomer.DataSource = Nothing
            da = New SqlDataAdapter(sqlCommand)
            dt.Clear()
            Try
                da.Fill(dt)
                Me.mydgCustomer.DataSource = dt

            Catch ex As Exception
                MsgBox(ex.Message & " " & ex.Source)
            End Try

        Catch ex As ArgumentOutOfRangeException
            Exit Sub

        End Try
0
 
LVL 16

Expert Comment

by:13598
Comment Utility

You need to refresh it more?
I don't quite follow the logic here but if that is the case then use the row_validating event to put your code in. Anytime something changes in any row it will refresh.
0
 
LVL 16

Expert Comment

by:13598
Comment Utility
Our posts crossed.
You double-click on a row to update, the form comes up, you update values, then form closes and you want to refresh.
If you are doing any validation use the rowvalidating event since you can cancel the update if any errors.
If you are not doing any validation use the rowvalidated event to do your refresh.
0
 

Author Comment

by:gloriagalvez
Comment Utility
I just tried the GotFocus form event and it worked!! When the Activated event did not.  would the gotfocus event is safe to use for this purpose?
0
 
LVL 17

Accepted Solution

by:
Zhaolai earned 500 total points
Comment Utility
Change the following line:

frmDetail.Show()

To

frmDetail.ShowDialog()


When using frmDetail.Show(), the code after it will continue being executed after the form shows, while frmDetail.ShowDialog() shows a modal form, meaning the code after it will get executed only after the forms closes.

0
 

Author Closing Comment

by:gloriagalvez
Comment Utility
Excellent!! Thank you so much Zhaolai.
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
If you haven’t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.  You will also find the …
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now