Home All Groups Group Topic Archive Search About

proper way to update a datagrid from a separate thread (bringtofront error vs versionnotfoundexcepti

Author
19 Mar 2005 6:57 PM
lloyd

i have a datagrid that i need to be updated every couple seconds in a
point-of-sale type application.  i thought this was going to be easy but
apparently its not.  i have a simple sql query to select a few columns from
a table and i want these to end up in a datagrid.

first time around i had a thread doing a fill as follows:

Dim da As New SqlClient.SqlDataAdapter("select c.clientid
,c.balance,c.connected,isnull(ac.security,0) creditid from client c left
join acctcredit ac on ac.creditid=c.creditid", sqlConn)
m_DataSet = New DataSet

da.Fill(m_DataSet, "Client")
Form.BeginInvoke(CallDataBindToDataGrid)

where calldatabindtodatagrid is:

Form.DataGridClients.DataSource = m_DataSet

but the result of that was that whenever the datagrid refreshed it both
reset the scroll position, and more important the form would be brought to
the
foreground as if bringtofront was called which caused major problems and so
this was obviously unacceptable.

for my next try i decided to just access the datatable directly with the
following code every period:

Me.DataTable1.BeginLoadData()
For i As Integer = 0 To DataTable1.Rows.Count - 1
    DataTable1.Rows(i).ItemArray = DirectCast(ar(i), DataRow).ItemArray
Next
Me.DataTable1.EndLoadData()
Me.DataGridClients.Refresh()

but every so often this gives me an exception versionnotfoundexception
"there is no original data to access" which i have yet to be able to find a
workaround/fix for.  its often enough that i'm working on a saturday morning
to fix it.  so far my google's/newsgroup searches have found alot of other
people with the same problem but apparently no way around it.

so rather than keep on trying to fix specific errors or experimenting with
different ways of updating the datagrid, i ask:  what is the right way to do
this.  my goal is simply to have datagrid1 update every couple seconds in a
background thread.  im still amazed how difficult this has proven but
hopefully im just missing something major and someone can point me in the
direction.  thanks for putting up with the rather long and rambling post
too.

Lloyd
Author
21 Mar 2005 8:18 PM
Sahil Malik
Try this -

Do a DataGrid.DataSource = DataSet out of all other code (i.e. define a
structure to the dataset, and databind it - it'll show no rows at this
point).

Then fire up the background thread.
That background thread will do dataadapter.fill on a similar dataset.
Then do Form.Invoke (I think it is in ISyncObject) to do a thread context
switch, and on the thread of the form call a dataset.merge (don't do
datasource = dataset at this time).

.... That's  one approach.

Another is, just use a timer on one form.

- Sahil Malik
http://codebetter.com/blogs/sahil.malik/





Show quoteHide quote
"lloyd" <leve***@novuscom.net> wrote in message
news:966dnYOGz66166HfRVn-oA@novus-tele.net...
> i have a datagrid that i need to be updated every couple seconds in a
> point-of-sale type application.  i thought this was going to be easy but
> apparently its not.  i have a simple sql query to select a few columns
from
> a table and i want these to end up in a datagrid.
>
> first time around i had a thread doing a fill as follows:
>
> Dim da As New SqlClient.SqlDataAdapter("select c.clientid
> ,c.balance,c.connected,isnull(ac.security,0) creditid from client c left
> join acctcredit ac on ac.creditid=c.creditid", sqlConn)
> m_DataSet = New DataSet
>
> da.Fill(m_DataSet, "Client")
> Form.BeginInvoke(CallDataBindToDataGrid)
>
> where calldatabindtodatagrid is:
>
> Form.DataGridClients.DataSource = m_DataSet
>
> but the result of that was that whenever the datagrid refreshed it both
> reset the scroll position, and more important the form would be brought to
> the
> foreground as if bringtofront was called which caused major problems and
so
> this was obviously unacceptable.
>
> for my next try i decided to just access the datatable directly with the
> following code every period:
>
> Me.DataTable1.BeginLoadData()
> For i As Integer = 0 To DataTable1.Rows.Count - 1
>     DataTable1.Rows(i).ItemArray = DirectCast(ar(i), DataRow).ItemArray
> Next
> Me.DataTable1.EndLoadData()
> Me.DataGridClients.Refresh()
>
> but every so often this gives me an exception versionnotfoundexception
> "there is no original data to access" which i have yet to be able to find
a
> workaround/fix for.  its often enough that i'm working on a saturday
morning
> to fix it.  so far my google's/newsgroup searches have found alot of other
> people with the same problem but apparently no way around it.
>
> so rather than keep on trying to fix specific errors or experimenting with
> different ways of updating the datagrid, i ask:  what is the right way to
do
> this.  my goal is simply to have datagrid1 update every couple seconds in
a
> background thread.  im still amazed how difficult this has proven but
> hopefully im just missing something major and someone can point me in the
> direction.  thanks for putting up with the rather long and rambling post
> too.
>
> Lloyd
>
>
>

Bookmark and Share