Link to home
Start Free TrialLog in
Avatar of JamesBrian
JamesBrian

asked on

Refresh datagrid using timer event

Hi all,

I am filling my Datagrid like this :

    Private Sub LoadData()
        'Construct data adapter
        SelectCmdString = "select * from SMSQUEUE WHERE Acknowledged=0 order by SMSQueueID"
        da = New SqlDataAdapter(SelectCmdString, conn)
        da.Fill(ds, "SMSQUEUE")
        LabelID.DataBindings.Add("Text", ds, "SMSQUEUE.smsqueueid")
        LabelDate.DataBindings.Add("Text", ds, "SMSQUEUE.createdon")
        LabelDestination.DataBindings.Add("Text", ds, "SMSQUEUE.destination")
        LabelDescription.DataBindings.Add("Text", ds, "SMSQUEUE.description")
        DataGrid1.SetDataBinding(ds, "SMSQUEUE")
        Timer1.Interval = CInt(myInterval) * 1000
        Timer1.Enabled = True

    End Sub

So far so good.

In my timer event I want to refresh the data in the datagrid:

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
         LoadData()
End Sub

I have tried setting da= nothing and ds=nothing but that did'nt work.

How can I refresh the data without getting errormessage : This would cause two bindings in the collection to bind to the same property. Parameter name: binding



Avatar of Sancler
Sancler

It looks as though your dataadapter - da - is declared outside your LoadData sub.  That being so, you don't need to keep reinitialising it every time you want to refresh your data.  Nor, having bound the controls once, do you need to keep re-binding them.  Their contents will update automtically as the datatable's data does.  So just call the LoadData sub once, at the start, and then replace

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
         LoadData()
End Sub

with

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
         da.Fill(ds, "SMSQUEUE")
End Sub

Roger
ASKER CERTIFIED SOLUTION
Avatar of Sancler
Sancler

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial