• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 708
  • Last Modified:

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

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
gloriagalvez
Asked:
gloriagalvez
  • 6
  • 4
  • 4
  • +1
1 Solution
 
ZhaolaiCommented:
Change the Refresh button click event to a sub.
 Call the sub right after your update form's ShowDialog.
0
 
13598Commented:
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
 
raaziqCommented:
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
gloriagalvezAuthor Commented:
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
 
13598Commented:
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
 
ZhaolaiCommented:
Combine @13598's with my first comment. Add the following line:

        btnRefresh.PerformClick()

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


0
 
gloriagalvezAuthor Commented:
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
 
ZhaolaiCommented:
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
 
gloriagalvezAuthor Commented:
The update of my datagridview then only happens when i open the form again.
0
 
gloriagalvezAuthor Commented:
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
 
13598Commented:

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
 
13598Commented:
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
 
gloriagalvezAuthor Commented:
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
 
ZhaolaiCommented:
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
 
gloriagalvezAuthor Commented:
Excellent!! Thank you so much Zhaolai.
0

Featured Post

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

  • 6
  • 4
  • 4
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now