locked
DataGridView not updating RRS feed

  • Question

  • I am testing the design capabilities in VS 2017 using VB.Net.  I am trying to convert a MS Access Front End to vb.net.  In Access I have a subform on a page that continually updates and displays the results of a query to show employees clocked in or out.

    In VB.Net I created a simple form and added a datagridview and using the wizard connected it to my access .accdb database.

    When I run the app it shows the data but as people clock in and out it will not show the changes.  I have tried for 2 weeks now to find a way to refresh the datasource to update these values. Should I use a different approach or is there a simple way to refresh the data in the dataset?

    Wednesday, August 9, 2017 8:54 AM

Answers

  • Hi Snydl0ga,

    I use Timer to refresh the datagridview data in the specific time interval, please refero to the code below.

    Private Sub Form6_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            'loaddata()
            load()
            Timer1.Enabled = True
        End Sub
       
        Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
            load()
        End Sub
        Private Sub load()
            Dim str As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\Database\InsertDate.accdb;Persist Security Info=False;"
            Dim dt As New DataTable()
            Dim bs As New BindingSource()
            Dim conn As New OleDbConnection(str)
            Dim sql As String = "Select * from Table1"
            Using cmd As New OleDbCommand(sql, conn)
                Dim adapter As New OleDbDataAdapter(cmd)
                adapter.Fill(dt)
                bs.DataSource = dt
                DataGridView1.DataSource = bs
            End Using
        End Sub
    
        Private Sub Form6_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
            Timer1.Enabled = False
        End Sub

    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 Snydl0ga Thursday, August 10, 2017 12:40 PM
    Thursday, August 10, 2017 3:19 AM
  • I added the Imports statement and that fixed that issue.  This code is working and I appreciate it greatly.

    Snydl0ga

    • Marked as answer by Snydl0ga Thursday, August 10, 2017 12:40 PM
    Thursday, August 10, 2017 12:40 PM

All replies

  • One of the quirks with using the wizards (and especially with ms-access) is that if all data operations such as adding data is done via the DataGridView is you need to repeat the process done in form load event e.g. SomeTableAdapter.Fill 

    So let's say I have 10 records that I'm viewing and someone adds a record, it will not appear unless the process is repeat as per above say in a timer or button click event. Here I use a button to manually refresh the data. Of course if this is done the current record will not be current anymore, if this is important you can get the current record primary key value via casting the BindingSource.Current property to a DataView then use Row.Field(Of Integer) to remember the value. Once re-loaded located that primary key using BindingSource.Find which returns -1 if not located or the row position. Use BindingSource.Position to set that row as current.

    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            'TODO: This line of code loads data into the 'ForumExamplesDataSet.Departments' table. You can move, or remove it, as needed.
            Me.DepartmentsTableAdapter.Fill(Me.ForumExamplesDataSet.Departments)
        End Sub
        Private Sub DepartmentsBindingNavigatorSaveItem_Click(sender As Object, e As EventArgs) Handles DepartmentsBindingNavigatorSaveItem.Click
            Me.Validate()
            Me.DepartmentsBindingSource.EndEdit()
            Me.TableAdapterManager.UpdateAll(Me.ForumExamplesDataSet)
        End Sub
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Me.DepartmentsTableAdapter.Fill(Me.ForumExamplesDataSet.Departments)
        End Sub
    End Class


    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


    Wednesday, August 9, 2017 10:23 AM
  • I am testing the design capabilities in VS 2017 using VB.Net.  I am trying to convert a MS Access Front End to vb.net.  In Access I have a subform on a page that continually updates and displays the results of a query to show employees clocked in or out.


    Are you sure of that, because a subform is not a program. It is a window on the screen which don't get the messages  from the messagesystem (messageloop, messagepump) from the OS. That is sent to the main form (active form)

    It is simply not a good way to use a subform for this kind of operations. 


    Success
    Cor


    Wednesday, August 9, 2017 11:51 AM
  • My Subform is in the old Access Front end, I just thought a DataGridView would work in my new vb.net app so that it always showed all employees clocked in/out.  I added a timer and put the Adapter.Fill code that is used in the Form Load but that did not work.

    I just want the whole datagridview to refresh once every few seconds to show me when some one clocks in or out.

    The data in the table is changed from any number of timeclock apps and since this view is just to read the data, data is not changed thru the dataset, it is just viewed.

    snydl0ga




    • Edited by Snydl0ga Wednesday, August 9, 2017 10:39 PM
    Wednesday, August 9, 2017 10:35 PM
  • I tried the above code and I can not get the "Me.TableAdapterManager" to validate.

    snydl0ga

    Wednesday, August 9, 2017 10:48 PM
  • I just want the whole datagridview to refresh once every few seconds to show me when some one clocks in or out.

    You'll want to set up a class library which has a class for your data (from your DAL).

    That class will implement the INotifyPropertyChanged interface and with that, with the UI - your presentation layer - subscribe to the PropertyChanged event and update the data.

    That way you're not needlessly "refreshing" the DGV every few seconds; you update it only when someone has logged in or out.


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Wednesday, August 9, 2017 11:00 PM
  • Take a look at the following code, the full project can be downloaded here.

    Imports System.ComponentModel
    Public Class Form1
        Private Sub CustomerBindingNavigatorSaveItem_Click(sender As Object, e As EventArgs) _
            Handles CustomerBindingNavigatorSaveItem.Click
    
            Validate()
            CustomerBindingSource.EndEdit()
            TableAdapterManager.UpdateAll(Me.DataSet1)
        End Sub
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            'TODO: This line of code loads data into the 'DataSet1.Customer' table. You can move, or remove it, as needed.
            CustomerTableAdapter.Fill(Me.DataSet1.Customer)
            CustomerTableAdapter.ClearBeforeFill = True
            Timer1.Enabled = True
        End Sub
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Close()
        End Sub
        Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
            If Not CustomerDataGridView.CurrentRow.IsNewRow Then
                Dim currentColumn As Integer = CustomerDataGridView.CurrentCellAddress.X
                Dim Identifier As Integer = CType(CustomerBindingSource.Current, DataRowView).Row.Field(Of Integer)("Identifier")
                CustomerTableAdapter.Fill(Me.DataSet1.Customer)
                Dim Postion As Integer = CustomerBindingSource.Find("Identifier", Identifier)
                If Postion > -1 Then
                    CustomerBindingSource.Position = Postion
                    CustomerDataGridView.CurrentCell = CustomerDataGridView(currentColumn, Postion)
                End If
            End If
        End Sub
        Private Sub Form1_Closing(sender As Object, e As CancelEventArgs) Handles Me.Closing
            Timer1.Enabled = False
        End Sub
    End Class
    


    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

    Thursday, August 10, 2017 12:08 AM
  • Hi Snydl0ga,

    I use Timer to refresh the datagridview data in the specific time interval, please refero to the code below.

    Private Sub Form6_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            'loaddata()
            load()
            Timer1.Enabled = True
        End Sub
       
        Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
            load()
        End Sub
        Private Sub load()
            Dim str As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\Database\InsertDate.accdb;Persist Security Info=False;"
            Dim dt As New DataTable()
            Dim bs As New BindingSource()
            Dim conn As New OleDbConnection(str)
            Dim sql As String = "Select * from Table1"
            Using cmd As New OleDbCommand(sql, conn)
                Dim adapter As New OleDbDataAdapter(cmd)
                adapter.Fill(dt)
                bs.DataSource = dt
                DataGridView1.DataSource = bs
            End Using
        End Sub
    
        Private Sub Form6_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
            Timer1.Enabled = False
        End Sub

    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 Snydl0ga Thursday, August 10, 2017 12:40 PM
    Thursday, August 10, 2017 3:19 AM
  • Hi Frank,

    The OP is using the IDE data wizards which means there is a constraint in that the objects the wizard created are private to the form so it would be very difficult to use a class here. This is one reason I don't advise using wizards along with other reasons that can constrain development. If they used a managed data providers such as OleDb then what you suggested would work.


    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

    Thursday, August 10, 2017 9:59 AM
  • Hi Cherry,

    The OP is using the IDE data wizards which means there is a constraint in that the objects the wizard created are private to the form so this means the OP can't use your code, only the timer part which I have already shown and provided a fully functional project to download.


    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

    Thursday, August 10, 2017 10:01 AM
  • Hi Frank,

    The OP is using the IDE data wizards which means there is a constraint in that the objects the wizard created are private to the form so it would be very difficult to use a class here. This is one reason I don't advise using wizards along with other reasons that can constrain development. If they used a managed data providers such as OleDb then what you suggested would work.


    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


    Databases aren't my strong suit so I'll defer to you on this, but it's a shame - an event is what's really needed here.

    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Thursday, August 10, 2017 11:33 AM
  • Thanks for your help.  I have added the above code but it tells me that "OleDbConnection" is not defined.

    Snydl0ga

    Thursday, August 10, 2017 12:08 PM
  • I added the Imports statement and that fixed that issue.  This code is working and I appreciate it greatly.

    Snydl0ga

    • Marked as answer by Snydl0ga Thursday, August 10, 2017 12:40 PM
    Thursday, August 10, 2017 12:40 PM
  • Wow, I'm confused, you indicated using the ide wizard to connect to data and then selected a reply that does not use the ide wizard, interesting.


    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

    Thursday, August 10, 2017 12:53 PM
  • For Clarification, I used the Form6 code. Just had to add the Import statement at the top

    Snydl0ga

    Thursday, August 10, 2017 3:14 PM