none
Automatic refresh of a query in a datagridview RRS feed

  • Question

  • Hi,

    How can I automatic refresh a query in a datagridview?

    Thanks in advance.

    Regards.


    Friday, November 3, 2017 7:21 PM

Answers

  • Hi Eduardo060888,

    As Kareninstructor said that there are two method to do this, one is to use Timer, another is to use Sqldependency class. Using Timer is easy to do, like this:

    Private Sub Form9_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim timer As New Timer
            timer.Interval = 3000
            AddHandler timer.Tick, AddressOf timer_Tick
            loaddate()
            timer.Start()
        End Sub
        Private Sub loaddate()
            Dim Str As String = "Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=D:\C# and VB Support\Example(VB)\Sample\Data1.mdf;Integrated Security=True"
            Dim sql As String = "select * from Test10"
            Dim dt As New DataTable
            Using con As New SqlConnection(Str)
                con.Open()
                Using cmd As New SqlCommand(sql, con)
                    Dim adapter As New SqlDataAdapter(cmd)
                    adapter.Fill(dt)
                    DataGridView1.DataSource = dt
                End Using
            End Using
        End Sub
        Private Sub timer_Tick(ByVal sender As Object, ByVal e As EventArgs)
            loaddate()
        End Sub

    But you can also use sqldependency class, this is a bit more complicated than a timer, you can choose one of them. You can  I find one sample that you can refer to:

    https://www.codeproject.com/Articles/144344/Query-Notification-using-SqlDependency-and-SqlCach

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Eduardo060888 Monday, November 6, 2017 7:28 PM
    Monday, November 6, 2017 3:26 AM
    Moderator

All replies

  • There is only one method when working with SQL-Server database which is SqlDependency class. Now the following is in C# but I'm not directing you there for code but instead how to setup SQL-Server to work with the SqlDependency class as the setup is not VB.NET or C#.

    Limitations for the above.

    Now you might say "I'm not using SQL-Server" which means you would need to roll your own solution as there is nothing for say MySql or MS-Access etc.

    Sure you could use a Timer but that is pretty hoocky.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Friday, November 3, 2017 9:17 PM
    Moderator
  • Hi Eduardo060888,

    As Kareninstructor said that there are two method to do this, one is to use Timer, another is to use Sqldependency class. Using Timer is easy to do, like this:

    Private Sub Form9_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim timer As New Timer
            timer.Interval = 3000
            AddHandler timer.Tick, AddressOf timer_Tick
            loaddate()
            timer.Start()
        End Sub
        Private Sub loaddate()
            Dim Str As String = "Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=D:\C# and VB Support\Example(VB)\Sample\Data1.mdf;Integrated Security=True"
            Dim sql As String = "select * from Test10"
            Dim dt As New DataTable
            Using con As New SqlConnection(Str)
                con.Open()
                Using cmd As New SqlCommand(sql, con)
                    Dim adapter As New SqlDataAdapter(cmd)
                    adapter.Fill(dt)
                    DataGridView1.DataSource = dt
                End Using
            End Using
        End Sub
        Private Sub timer_Tick(ByVal sender As Object, ByVal e As EventArgs)
            loaddate()
        End Sub

    But you can also use sqldependency class, this is a bit more complicated than a timer, you can choose one of them. You can  I find one sample that you can refer to:

    https://www.codeproject.com/Articles/144344/Query-Notification-using-SqlDependency-and-SqlCach

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Eduardo060888 Monday, November 6, 2017 7:28 PM
    Monday, November 6, 2017 3:26 AM
    Moderator
  • Eduardo060888, 

    En qué correo te puedo contactar, tengo unas dudas respecto de las queries.

    a ver si me puedes ayudar.

    Gracias.

    Friday, April 27, 2018 12:18 AM
  • Eduardo060888, 

    En qué correo te puedo contactar, tengo unas dudas respecto de las queries.

    a ver si me puedes ayudar.

    Gracias.

    Eduardo060888,

    In what email I can contact you, I have some doubts about the queries.

    Let's see if you can help me.

    Thank you.

    Well it is unlikely that anybody will post their email address here so they won't get compromised by people wanting email addresses.

    Plus this is an english language Forum.

    If you have a question then begin a thread in English jparada and people will attempt to assist you with whatever issue(s) you are having.


    La vida loca

    Friday, April 27, 2018 12:25 AM