Refresh Grid without flash

Hi All,

I'm getting data from a database and displaying that information dynamically in silverlight.  At some point in time I'm calling a refresh function via a timer:

       Dim myDispatcherTimer As New System.Windows.Threading.DispatcherTimer()
        myDispatcherTimer.Interval = New TimeSpan(0, 0, 0, 0, 7000)
        AddHandler myDispatcherTimer.Tick, AddressOf Timer_Tick
        myDispatcherTimer.Start()

which calls this:
    Private Sub Timer_Tick(ByVal o As Object, ByVal sender As EventArgs)
        FloorGrid.Children.Clear() '
        LoadGrid()  ' reloads data
    End Sub

I'm really not looking to do a Children.Clear though as this causes the children to disappear and re-appear. So how do I associate incoming data with what is reloading (keeping in mind values may have inserted,updated,or deleted) and repaint without clearing?

Thanks in advanced for any help.


LVL 42
Kyle AbrahamsSenior .Net DeveloperAsked:
Who is Participating?
 
CodeCruiserConnect With a Mentor Commented:
What is the code in LoadGrid? Change it so that your sequence changes from

1)Clear Grid
2)Download Data
3)Repopulate grid

to
1)Download Data
2)Clear Grid
3)Repopulate Grid


This way, you would remove the time it takes to download the grid data and hopefully the flicker will be gone.
0
 
mikebirtCommented:
Hi,

i'm not sure i understand your situation completely, so first i'll clarify my assumption; you have some data collection that is being populated by a DB call - you are binding you collection to a grid, you want to update your collection and your grid to update automaticalyl without clearing and repopulating?

you should be able to achieve this by storing your data in an ObservableCollection. Bind you data grid to you observable collection. When you recieve data from the DB, manage your collection - remove stuff that's not present, replace stuff that has changed etc... your grid should update itself automatically from the collection. I've used this for populating stuff several times, i bind to the collection - which starts of empty, and as stuff gets put into the collection so my UI control content changes.

HTH

Mike
0
 
Kyle AbrahamsSenior .Net DeveloperAuthor Commented:
Thanks very much for the reply.  

My Grid in this case is just an actual grid.  

eg:  


<Grid ShowGridLines="false" x:Name="Grid1" Background="White">
</Grid>

I'm adding pictures to this grid:

dim baseURI as string
baseURI = "http://localhost/test/"
Dim uri As New Uri(baseURI + "Images/pic1.png")
Dim bmp As new Imaging.BitmapImage(uri)
Dim img As New Image
img.Source = bmp

Grid.SetColumn(img,2)
Grid.SetRow(img,1)

Grid1.Children.Add(img)

This works fine and doesn't change.

What's really weird is I have some borders I also use (I'm essentially using this for a rectangle with text at the moment), which does the same thing, however goes away and comes back.  The only thing that's different is that it comes from the service instead of local.

this is a snippet of code from my service_done sub.  I've cleaned it up for simplicity sake and wherever you see static values assume that's coming from the LinqToSQL.  As always the help is greatly appreciated.


Dim r As Border
Dim t As TextBlock

'in a loop later in the code:
r = new border
t = new textblock

r.Width = 20
r.Height = 40

Canvas.SetZIndex(Grid1, 100)
Canvas.SetZIndex(r, 50)

 r.BorderThickness = New Thickness(0.5)
 t.Foreground = New SolidColorBrush(Colors.White)

t.Text = "test"
r.Background = New SolidColorBrush(Colors.Blue)
r.Child = t

'make the rectangle moveable
                AddHandler r.MouseLeftButtonDown, AddressOf OnRectangleMouseDown
                AddHandler r.MouseLeftButtonUp, AddressOf OnRectangleMouseUp

                FloorGrid.Children.Add(r)


0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
Kyle AbrahamsSenior .Net DeveloperAuthor Commented:
The Last FloorGrid.Children.Add(r) should be:

 Grid1.Children.Add(r)
0
 
Kyle AbrahamsSenior .Net DeveloperAuthor Commented:
Observable collections may be the way to go.  Am reasearching them now but any help greatly appreciated.

Coming from the .Net world of Databinding so some of the concepts I'm reading about are new to me.  

Any good tutorials or how would I use this to display the data properly?
0
 
Kyle AbrahamsSenior .Net DeveloperAuthor Commented:
The whole grid is flashing instead of just the dynamic items now.

Makes sense what you did but this is a very intensive process and needs to be able to support 1000 iterations.  I believe the data needs to be updated in a workspace behind and then brought forth all at once.  

I was trying to create a second grid and then assign the first grid = second grid but that always left a blank workspace.

Any other suggestions of how this can be done in the background?
0
 
mikebirtCommented:
Hi,

another thought - can you see the entire grid at once? if you have thousands of images, surely they can't all be on screen at once? is it possible to only update the ones that are visible? keep your background data source updated but only apply updates to your UI as things are in view?

HTH

Mike
0
 
CodeCruiserCommented:
Try this custom grid which implements virtual mode

http://www.codeproject.com/KB/silverlight/SLDataGridStep1.aspx
0
 
Kyle AbrahamsSenior .Net DeveloperAuthor Commented:
MikeBirt,

That's what I'm after, is how to update the UI properly without deleting and reading everything.

Currently I just add them to the via   myGrid.Children.Add(myCanvas)  and then forget about them.  What's the best way of updating them . . . keeping in mind that each canvas has it's own data that needs to be displayed.  I'm a huge fan for the virtual mode which CC suggested, but not sure if I want to do it myself or go through the third party control.  

Overall I'm looking to learn more about how to properly manipulate data now that it's in a client / server model again (as opposed to the postback model) which is why I'm considering at least experiencing it once myself.

Never played with WPF and as I said a lot of the concept are new to me and fighting the huge learning curve that comes along with this.

Code Cruiser,

Thanks for the tutorial, looks really informative but my eyes are shot.  Will take a look at it Monday and get back to you if I have questions.  



Appreciate all the help guys.

0
 
Kyle AbrahamsSenior .Net DeveloperAuthor Commented:
CC:  The link was for a data grid and doesn't apply.  

I'm very much interested in the virtual mode concept though.  How do I know what's currently being displayed on my grid (not data grid) and then refresh just for that section?
0
 
Kyle AbrahamsSenior .Net DeveloperAuthor Commented:
This ended up working once I tuned the code.  Will raise a second question on the virtual part.
0
 
CodeCruiserCommented:
I dont think that would be an easy task. I think the article above tackles the issue well.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.