Quantcast
Channel: VBForums
Viewing all articles
Browse latest Browse all 15107

DataGrid Refresh

$
0
0
Afternoon,

I have 2 data grid views that have info read from a SQL DB

What I am trying to achieve is that records added to the SQL DB show in the data grid view when updated.

I am trying to do this by a timer with runs every minute

My data is generated from code as I need to run 2 SQL query's to populate the data i require


Code:

Dim ClockIn As String = ";WITH InWarehouse AS (SELECT ROW_NUMBER() OVER (PARTITION BY RFID ORDER BY Time DESC) AS RN, data.Initial, Data.RFID, Data.Time, Data.Type
        FROM Data)
        SELECT * FROM InWarehouse WHERE RN = 1 AND Type = 1 order by GETDATE() asc"
        Dim Sqlcon As SqlConnection = New SqlConnection("Data Source= PHONE-System\RFID;Initial Catalog=RFID;Integrated Security=True")
        'Dim sqlcmd As SqlCommand
        Dim Adapter As SqlDataAdapter
        Dim ds As New DataSet
        'DGVClockIn.AutoGenerateColumns = False
        Sqlcon.Open()
        Adapter = New SqlDataAdapter(ClockIn, Sqlcon)
        Adapter.Fill(ds)
        Sqlcon.Close()
        DGVClockIn.DataSource = ds.Tables(0)

        'Show Clocked Out Staff
        Dim ClockOut As String = ";with ClockIn as
        (select [Initial],[RFID],[Time] as ClockInTime from Data where Type=1 and [Time] >= DATEADD(dd, 0, DATEDIFF(dd, 0, GETDATE()))
        and [Time] < DATEADD(dd, 0, DATEDIFF(dd, 0, GETDATE()) + 1))
        ,ClockOut as
        (select [Initial],[RFID],[Time] as ClockOutTime from Data where Type=2 and [Time] >= DATEADD(dd, 0, DATEDIFF(dd, 0, GETDATE()))
        and [Time] < DATEADD(dd, 0, DATEDIFF(dd, 0, GETDATE()) + 1))
        select * from (
            select
            ClockIn.[Initial]
            ,ClockIn.[RFID]
            ,ClockIn.[ClockInTime]
            ,ClockOut.[ClockOutTime]
            ,ROW_NUMBER() over (
                partition by ClockIn.[Initial],ClockIn.[RFID],ClockIn.[ClockInTime]
                order by ClockIn.[ClockInTime] asc) as rownum
            from ClockIn
            inner join ClockOut
                on ClockOut.[Initial]=ClockIn.[Initial]
                and ClockOut.[RFID]=ClockIn.[RFID]
                and ClockOut.[ClockOutTime] > ClockIn.[ClockInTime]
        ) ClockCombined where rownum=1"
        Dim Adapter1 As SqlDataAdapter
        Dim ds1 As New DataSet
        'DGVClockedOut.AutoGenerateColumns = False
        Sqlcon.Open()
        Adapter1 = New SqlDataAdapter(ClockOut, Sqlcon)
        Adapter1.Fill(ds1)
        Sqlcon.Close()
        DGVClockedOut.DataSource = ds1.Tables(0)
        Timer3.Start()

Code:

    Private Sub Timer3_Tick(sender As Object, e As EventArgs) Handles Timer3.Tick
        'Update Data tables 1m
        DGVClockIn.Update()
        DGVClockIn.Refresh()
        DGVClockedOut.Update()
        DGVClockedOut.Refresh()

    End Sub


Viewing all articles
Browse latest Browse all 15107

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>