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

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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.


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.


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

My Grid in this case is just an actual grid.  


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

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



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


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

Why Diversity in Tech Matters

Kesha Williams, certified professional and software developer, explores the imbalance of diversity in the world of technology -- especially when it comes to hiring women. She showcases ways she's making a difference ithrough the Colors of STEM program.

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?
What is the code in LoadGrid? Change it so that your sequence changes from

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

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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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?

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?


Try this custom grid which implements virtual mode
Kyle AbrahamsSenior .Net DeveloperAuthor Commented:

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.

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?
Kyle AbrahamsSenior .Net DeveloperAuthor Commented:
This ended up working once I tuned the code.  Will raise a second question on the virtual part.
I dont think that would be an easy task. I think the article above tackles the issue well.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.