Click here to Skip to main content
15,847,427 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have one client programing which updates my table 'sharedfiles_tb'.
And i have another server program, which just shows all the rows in 'sharedfiles_tb'.

The server shows all the results in a datagridview with the help of a datatable. datatables is loaded with datareader.

But, when i add a row, through the client program, the new row added is not shown in the datagridview of the server immediately. If i restart the server program then it will be shown.

I created a thread which points to a function, which infinitely execute the query, but still the latest changes are not being reflected in the datagridview.

Following it the code

Dim dt As New DataTable
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
 Dim ThreadSharedFiles As New Threading.Thread(AddressOf GetSharedFiles)
End SUb
Private Sub GetSharedFiles()
        Dim dr As SqlDataReader
        Dim query As String
        Dim cmd As SqlCommand
        Dim conn As New SqlConnection
        While True
            If conn.State = ConnectionState.Open Then
            End If
            query = "select filename, size, username from sharedfiles_tb"
            conn.ConnectionString = "Data Source=hurricane\sqlexpress01;Initial Catalog=serverdb;Integrated Security=True"
            cmd = New SqlCommand(query, conn)
            dr = cmd.ExecuteReader()
            If dr.HasRows Then
                MessageBox.Show("No results found", "No Results", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            End If
        End While
End Sub
Private Sub PopulateSharedFilesDatagrid()
        If Me.InvokeRequired Then
            Me.Invoke(New MethodInvoker(AddressOf PopulateSharedFilesDatagrid))
            GroupBox2.Text = Label3.Text + " Shared Files"
            DataGridView1.DataSource = dt
        End If
    End Sub

please help....
Updated 14-Sep-10 4:57am

1 solution

You might have more luck by using a BindingSource Component.

Here[^] is a link to a page in MSDN with loads of good stuff about the BindingSource.

Basically you create a new BindingSource (lets call it myBindingSource just for this example).

Then in whatever method you do your basic setup for the form (FormLoad or the constructor) you put
DataGridView1.DataSource = myBindingSource

Then in your PopulateSharedFilesDatagrid() method replace
DataGridView1.DataSource = dt

myBindingSource.DataSource = dt
myBindingSource.ResetBindings(False) '<======== this line might not be needed try with it and without.

There are lots of advantages to using a BindingSource.
It makes sorting and filtering much, much easier. Same with error handling and propertychanged handling.

Good luck! :)
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900