Solved

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

Posted on 2010-08-26
15
703 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
ID: 33536554
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
ID: 33536624
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
ID: 33540179
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:gloriagalvez
ID: 33541838
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
ID: 33542561
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
ID: 33542708
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
ID: 33542823
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
 
LVL 17

Expert Comment

by:Zhaolai
ID: 33542917
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
ID: 33543035
The update of my datagridview then only happens when i open the form again.
0
 

Author Comment

by:gloriagalvez
ID: 33543053
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
ID: 33543067

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
ID: 33543112
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
ID: 33543143
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
ID: 33543145
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
ID: 33543627
Excellent!! Thank you so much Zhaolai.
0

Featured Post

Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows Script Host (WSH) has been part of Windows since Windows NT4. Windows Script Host provides architecture for building dynamic scripts that consist of a core object model, scripting hosts, and scripting engines. The key components of Window…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

733 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