# .NET and More > Silverlight >  [RESOLVED] DataGrid not updating when data is updated

## HerkyBird

Newbie at Silverlight, working in Silverlight 3 & Visual Studio 2010, and I've reached the "bang head against wall" point with a small project.  What I've got is some data in the LineList variable, and while I want to display all of it in the datagride, for now I want to manipulate it (specifically, adding lines) with code that executes when a button is clicked, rather than having the user fiddle with the DataGrid.

The existing code displays the initial two lines that are populated when the button btnPopulateTestData is clicked (although for some reason I can't accomplish this when everything starts; it has to be a separate click -- not the way I want it to work eventually).  However, when the button btnInsertLine is clicked, adding a new line of data to the LineList, nothing seems to update on the DataGrid, and I've exhausted my resources of searching & reading without finding what I'm doing wrong.  It's probably extremely simple, but I'm missing it.

"Help me, Obi-Wan Kenobi, you are my only hope!"

Many thanks,

Herky Bird



```
Option Explicit On
Option Strict On

Partial Public Class MainPage
    Inherits UserControl

    Public Shared LineList As List(Of BidLineEntry) = New List(Of BidLineEntry)

    Public Sub New()
        InitializeComponent()
        ' InitialPopulate()

        ' *** *** *** The Initial Populate routine works fine when called as a separate button click,
        '             but when I call it from here (which is what I'd like -- get the first records in
        '             & displayed when the user first sees everything) I get an error.  Don't know why.


    End Sub

    Private Sub btnPopulateTestData_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnPopulateTestData.Click

        InitialPopulate()

    End Sub
    
    	Public Sub InitialPopulate()
    
            LineList.Add(New BidLineEntry() With {.CursorSymbol = ">>_", .HardResBlank = "", .LineNumber = "", .Rank = "", .Source = ""})
    
            LineList.Add(New BidLineEntry() With {.CursorSymbol = "", .HardResBlank = "H", .Rank = "1", .LineNumber = "26", .Source = "auto"})
    
    
            MainDataGrid.ItemsSource = LineList
            MainDataGrid.UpdateLayout()
            MainDataGrid.Columns(0).Header() = "____"
    
            MainDataGrid.Columns(2).Header() = "Line #"
    
            MainDataGrid.Columns(3).Header = "H/R/B"
    
            MainDataGrid.ColumnWidth = DataGridLength.SizeToHeader
            MainDataGrid.UpdateLayout()
    
            MainDataGrid.Columns(0).Header() = ""
    
        End Sub
    
        Private Sub btnInsertLine_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnInsertLine.Click
    
    
            LineList.Add(New BidLineEntry() With {.CursorSymbol = "", .Rank = "1.", .LineNumber = "77", .HardResBlank = "H", .Source = "Keyboard"})
            MainDataGrid.ItemsSource = LineList
    
            MainDataGrid.UpdateLayout()
    
            ' *** *** *** I'd expect that the new entry in the LineList would show up at this point in the grid,
            '             but it does not.  Not sure why not.  The line below shows that the LineList is, in fact,
            '             getting bigger, but the grid never reflects the new entries. WUWT?
    
            tboxMsgBox.Text = LineList.Count.ToString
    
        End Sub
        
 Public Class BidLineEntry
        Private _cursorsymbol As String
        Public Property CursorSymbol As String
            Get
                Return _cursorsymbol
            End Get
            Set(ByVal value As String)
                _cursorsymbol = value
            End Set
        End Property

        Private _rank As String

        Public Property Rank As String
            Get
                Return _rank
            End Get
            Set(ByVal value As String)
                _rank = value
            End Set
        End Property

        Private _linenumber As String
        Public Property LineNumber As String
            Get
                Return _linenumber
            End Get
            Set(ByVal value As String)
                _linenumber = value
            End Set
        End Property

        Private _hardresblank As String
        Public Property HardResBlank As String
            Get
                Return _hardresblank
            End Get
            Set(ByVal value As String)
                _hardresblank = value
            End Set
        End Property

        Private _source As String
        Public Property Source As String
            Get
                Return _source
            End Get
            Set(ByVal value As String)
                _source = value
            End Set
        End Property

    End Class
End Class
```

----------


## MattP

Rather than a List(Of BidLineEntry) you should be using an ObservableCollection(Of BidLineEntry) since it implements INotifyPropertyChanged.

----------


## HerkyBird

AAAaaahhhh!

That would have taken me a while (like, several months) to figure that out on my own.  Thank you for the help!  I've got the beginning of the code to implement that written, and already I can see that this does what I needed.  After I polish it a bit more, I will try to address why the sub that initially populates the grid isn't working with the initialize call but needs a separate button click to function.  But I'll finish this tweak before I get too wrapped up in that, unless the cause/solution is an obvious one from what I've posted already.

Thanks again for the help, Matt!

H.B.

----------

