Avatar of Jimbo99999
Jimbo99999
Flag 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
Visual Basic.NET

Avatar of undefined
Last Comment
John (Yiannis) Toutountzoglou

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
John (Yiannis) Toutountzoglou

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Jimbo99999

ASKER
Yes, excellent I forgot about the Timer. I will try now.
John (Yiannis) Toutountzoglou

and
 Dim m_Interval As Integer = 60
John (Yiannis) Toutountzoglou

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

I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Jimbo99999

ASKER
It is working...excellent.  

Thanks for the help,
jimbo99999
John (Yiannis) Toutountzoglou

Thank you too ...!!!