How to use threading to UPdate Datagridview with timer

brucelim80

Active member
Joined
Apr 20, 2006
Messages
35
Programming Experience
Beginner
Hi
Can anyone expert here help me with this problem?

I have the code below to update the dataset
Dim myConnects4 As New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\\136.121.2.1\its helpdesk\calltrack\MDB\ecall.mdb;")
myConnects4.Open()
Dim personaljobadapter As OleDbDataAdapter = New OleDbDataAdapter("SELECT * FROM TrackingDB WHERE assignto='" & currentuser & "'", myConnects4)
personaljobadapter.Fill(DataSet1.Searchtrack)
myConnects4.Close()


On the other hand,i have a Bindingsource("Searchdata")which datasource is linked to the dataaset above is mapped to the datagridview as the datasource

When i use the timer to update the bindingsource and refreshing the datagridview for every two sec, it will slow down my application.


How do i write in such a way that my datagridview will be updated within every 2sec and it does not affect my application? How do i use a threading ?




Thank you,
Regards,
Bruce
 
First, let me suggest that you change your profile to reflect that you're using .NET 2.0 rather than .NET 1.1.

As to your question, just use a BackgroundWorker and populate the table in its DoWork event handler. You'd call its RunWorkerAsync method from the Timer's Tick event handler, after first testing its IsBusy property.
 
method cannot work

Thank for your respond

i had try the method as mentioned however it prompt me the error that the datasource of the datagridview cannot be refer back to the BindingSource.

You had mentioned the IsBusy property.
How do i code it?


Can you help me on this?

second question.
how do i code in such a way that when i clicked on the first cell of first colummn and it will reflect the value on the textbox of the form?
 
I'm sorry but vague interpretations of error messages are rarely of any use. Give us the EXACT error message as it was given to you by the IDE. If it's not in English then provide the closest translation you can, because this:
the datasource of the datagridview cannot be refer back to the BindingSource
means nothing to me.
 
hi jmcihinney
i had solve the problem.

However, i am still facing a Dilemma that i cannot solve.
Can you advise me on this?

I had a textbox and a datagrdview bind to this dataset with the code below.
But when i had the code dataset1.searchtrack.clear to be initialized every two sec using timer. it caused the whole dataset to refresh and do not allow selection of the row in the datagridview as well as resetting the selected value.

How do i code in such a way that every two sec it refresh the data and do not affect the selection of the row of the datagridview?




Dim myConnects4 As New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLED B.4.0;Data Source=\\136.121.2.1\its helpdesk\calltrack\MDB\ecall.mdb;")
myConnects4.Open()
Dim personaljobadapter As OleDbDataAdapter = New OleDbDataAdapter("SELECT * FROM TrackingDB WHERE assignto='" & currentuser & "'", myConnects4)
dataset1.searchtrack.clear()
personaljobadapter.Fill(DataSet1.Searchtrack)
myConnects4.Close()
 
If you have a typed DataSet then you shouldn't be using a DataAdapter at all. You should be using a TableAdapter, which will clear a DataTable before Filling by default.
 
Hi Jmclilhinney,

Thanks for your respond. however i still face problem.
I can only create the tableadpater at the design mode.
How do i write the code in the coding window?

Secondly i will like to have a SQL statement with its condition link to a variable but the table adapater is unable to do it. i am using microsoft access database
("SELECT * FROM TrackingDB WHERE assignto='" & currentuser & "'")

Please kindly help me with this.

Thank you for your great help.
 
You should be adding a query to your TableAdapter in the designer; one that takes an AssignTo value. You would name the methods associated with the query FillByAssignTo and GetDataByAssignTo. You'd then fill an existing DataTable by AssignTo like so:
VB.NET:
Using adapter As New TrackingDBTableAdapter
    adapter.FillByAssignTo(myDataSet.TrackingDB, currentUser)
End Using
To add a query to a Tableadapter you just right-click it in the designer and select the appropriate option.
 
hi Jmclilhinney,

I had placed the code with the following code as advise below

However, when i put a timer to update the dataset, it always reset my selection on the datagridview.
I tried to remove the cleartable option at the tableadapter but it doesnot update my dataset.
When one record is set to another name from another form, it will be remove from the view.

Please advise on how to solve this problem
i want the selection of the datagird to hold and the bindingtexbox to stay on the selection there when the dataset is refreshed




VB.NET:
Public Class personalportal
    Dim data As String = "Bruce Lim"

    
    Private Sub personalportal_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Timer1.Start()

    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        TrackingDB1TableAdapter.FillByASSIGNTO(EcallDataSet.TrackingDB1, data)
    End Sub
End Class




Example i have the following data in the database which will be show on the datagirdview on PC A


no recordid task assignto
1 SIB744ZZ CallData brucelim
2 SIB56444 Dataentry brucelim
3 SIB23E55 Report brucelim


Then another user accessing the same program on another PC B update the record on the same database

no recordid task assignto
1 SIB744ZZ CallData brucelim
2 SIB56444 Dataentry John
3 SIB23E55 Report brucelim

He had admended the second record with the name john




PC A must be able to see the changes and his selection on the datagrid cannot be interrupted.
Please advise how do i solve this issue.
 
Last edited by a moderator:
Of course it resets your selection, because all the old rows are discarded and new ones created. If you want to maintain your selection then it's up to you to store the IDs of the selected rows first, then reselect the corresponding rows after repopulating the data.
 
hi jim,

Do you have a sample of the code as i find it vague or where can i find from the web?
sorry i am quite new to this language.

How do i mantain the selection and how do i store the id of the selected row?

Thank you for your great help and patience
 
Back
Top