Link to home
Avatar of Jimbo99999
Jimbo99999Flag for United States of America

asked on

VB.Net - Refresh DataGridView Dynamically

Good Day Experts!

I have an interest for my applicaiton that "feels" like it is possible but not quite sure how to apporach it.  I have a basic setup here...DataGridView that gets loaded with SQL table data on the click of a button.  

I want to add a check box and a textbox for refreshing the DataGrid.  If the checkbox is checked and the textbox has a value for refreshing interval, then after the initial button click the DataGridView will refresh without having to hit the button again.

Does this sound possible?  I just can't figure how to click the button programatically every X number of seconds.

Thanks,
jimbo99999
ASKER CERTIFIED SOLUTION
Avatar of John (Yiannis) Toutountzoglou
John (Yiannis) Toutountzoglou
Flag of Greece image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Jimbo99999

ASKER

Yes, excellent I forgot about the Timer. I will try now.
and
 Dim m_Interval As Integer = 60
Some Corrections....
Event Finished(ByVal sender As Object)
    Dim m_Interval As Integer = 60
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

        m_Interval -= 1
        RefreshBtn.Text = "Refresh Data in " & System.TimeSpan.FromSeconds(m_Interval).ToString.Substring(6) & " sec"
        If m_Interval = 0 Then
            Me.Cursor = Cursors.WaitCursor
            ' InitializeDataGridView()
            Timer1.Enabled = False
            RaiseEvent Finished(Me)
            m_Interval = 60
            Timer1.Enabled = True
            Me.Cursor = Cursors.Default
        Else

            RefreshBtn.Text = "Refresh Data in " & System.TimeSpan.FromSeconds(m_Interval).ToString.Substring(6) & " sec"

        End If
    End Sub
    Private Sub RefreshIntervalChkBox_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles RefreshIntervalChkBox.CheckedChanged
        If Me.RefreshIntervalChkBox.CheckState = CheckState.Checked Then
            Timer1.Interval = 1000
            Timer1.Enabled = True
            m_Interval = 60
            RefreshBtn.Text = "Refresh Data in " & System.TimeSpan.FromSeconds(m_Interval).ToString.Substring(6) & " sec"
        Else
            Me.RefreshBtn.Text = "Refresh"
            Timer1.Enabled = False
            RaiseEvent Finished(Me)
        End If
    End Sub

    Private Sub RefreshBtn_Click(sender As System.Object, e As System.EventArgs) Handles RefreshBtn.Click
        Me.Cursor = Cursors.WaitCursor
        '    InitializeDataGridView()
        Timer1.Enabled = False
        RaiseEvent Finished(Me)
        m_Interval = 60
        Timer1.Enabled = True
        Me.Cursor = Cursors.Default
    End Sub

Open in new window

It is working...excellent.  

Thanks for the help,
jimbo99999
Thank you too ...!!!