Refresh Datagrid

Aug 6, 2010 at 11:17 AM
Edited Aug 6, 2010 at 11:19 AM

Hi again all. I wonder if I can pick your brains.

I have a datagrid (From EF) which is actually a View from SQL Server 2005. By clicking on a record in the datagrid I take the user into that record and he/she can make updates via db.savechanges. One thing I do is change a status field which is showing on the datagrid. I am having problems updating the datagrid to reflect any changes made.

Private Sub sendReport()

    Dim reportSheet As tblReportSheet = Me.ReportData.View.CurrentItem
    Try
      reportSheet.RS_CallStatus = "WAIT"
      reportSheet.RS_ActualSent = Now
      dbx.SaveChanges()

    Catch ex As Exception
      MessageBox.Show(ex.Message & vbCrLf & vbCrLf & ex.Source)
    End Try

  End Sub
The datagrids itemsource is a CollectionViewSource. Populated like so;
Private Sub PopulateListOfCalls()

    Dim status As New List(Of String)
    status.Add("WAIT")
    status.Add("OPEN")
    Dim list As IQueryable(Of View_EngineerManage) = From x In dbx.View_EngineerManage Where x.RS_EngineerName = User.User AndAlso status.Contains(x.RS_CallStatus)
    'Convert to observable collection so you can filter the list

    For Each item As View_EngineerManage In list
      obList.Add(item)
    Next
    Me.EngListViewSource = CType(Me.FindResource("DataActiveCalls"), CollectionViewSource)
    EngListViewSource.Source = obList

  End Sub

Any help? Many thanks Scott
Aug 6, 2010 at 3:38 PM

after dbx.SaveChanges()

call EngListViewSource.View.Refresh()

 

Aug 6, 2010 at 3:50 PM

Hi. Thanks for getting back to me. This has not worked. The datagrid remains in the same state.

I also tried it on another peice of code that add one record to a DB and still no joy.

Private Sub butAddNewRemedial_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
        If RemedialWork.View.CurrentPosition = -1 Then
            Dim newRecord As New tblRemedialWork

            newRecord.RW_CallNo = ViewManageRecord.RS_CallNo
            newRecord.RW_EngineerName = ViewManageRecord.RS_EngineerName
            newRecord.RW_DateAdded = Now
            newRecord.RW_Status = "ACTIVE"
            dbx.AddTotblRemedialWorks(newRecord)

            Try
                dbx.SaveChanges()
                RemedialWork.View.Refresh()
            Catch ex As Exception
                MessageBox.Show(ex.Message & vbCrLf & vbCrLf & ex.Source)
            End Try

        End If
    End Sub
Aug 6, 2010 at 4:16 PM

I got it working with the last bit of code. By requerying the linq query.

    Private Sub butAddNewRemedial_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
        If RemedialWork.View.CurrentPosition = -1 Then
            Dim newRecord As New tblRemedialWork

            newRecord.RW_CallNo = ViewManageRecord.RS_CallNo
            newRecord.RW_EngineerName = ViewManageRecord.RS_EngineerName
            newRecord.RW_DateAdded = Now
            newRecord.RW_Status = "ACTIVE"
            dbx.AddTotblRemedialWorks(newRecord)

            Try
                dbx.SaveChanges()
                RemedialWork.View.Refresh()

                Dim remData = From n In dbx.tblRemedialWorks
                      Where n.RW_CallNo = ViewManageRecord.RS_CallNo

                Me.RemedialWork = CType(Me.FindResource("RemedialWork"), CollectionViewSource)
                Me.RemedialWork.Source = remData


            Catch ex As Exception
                MessageBox.Show(ex.Message & vbCrLf & vbCrLf & ex.Source)
            End Try

        End If
    End Sub
But the datagrid will not refresh! Maybe because it's a VIEW that I'm using from SQL Server!
Aug 6, 2010 at 4:18 PM

Hi,

Can u confirm that ur RemedialWork table has the updated value before the view.refresh call? Also that the binding on the data grid is OneWay / TwoWay or AutoGeneratedColumns = True (if ur underlying source is data table.)?

Aug 6, 2010 at 4:21 PM

Have u bound the RemedialWork.View to the grid? or RemdialWork table itself? Try View.

Aug 6, 2010 at 4:35 PM

Hi.

I stopped the code at dbx.SaveChanges and the RemedialWork table has populated correctly.

The databindings are as follows;

<DataGrid.Columns>
                                        <DataGridTextColumn x:Name="RS_CallNo" Binding="{Binding RS_CallNo, Mode=OneWay}" Header="Call No" IsReadOnly="True" Width="Auto" Foreground="Red"/>
                                        <DataGridTextColumn x:Name="RS_DateAdded" Binding="{Binding RS_DateAdded, Mode=OneWay, StringFormat=dd/mm/yyyy}" Header="Date Added" IsReadOnly="True" Width="Auto"/>
                                        <DataGridTextColumn x:Name="Call_Site_Name" Binding="{Binding Call_Site_Name, Mode=OneWay}" Header="Site Name" IsReadOnly="True" Width="*"/>
                                        <DataGridTextColumn x:Name="Call_Site_Post_Code" Binding="{Binding Call_Site_Post_Code, Mode=OneWay}" Header="Postcode" IsReadOnly="True" Width="Auto"/>
                                        <DataGridTextColumn x:Name="RS_EngineerName" Binding="{Binding RS_EngineerName, Mode=OneWay}" Header="Engineer Name" IsReadOnly="True" Width="Auto" />
                                        <DataGridTextColumn x:Name="RS_ReportStatus" Binding="{Binding RS_ReportStatus, Mode=OneWay}" Header="Report Status" IsReadOnly="True" Width="Auto" Foreground="Firebrick"/>
                                        <DataGridTextColumn x:Name="RS_CallStatus" Binding="{Binding RS_CallStatus, Mode=TwoWay}" Header="Call Status" IsReadOnly="True" Width="Auto" Foreground="Blue"/>
                                    </DataGrid.Columns>

RS_CallStatus I just set to TwoWay and no difference. AutoGeneratedColumns = FALSE

RemedialWork data is NOT in a grid. I was just trying both avenues i.e. Normal data fields and a datagrid. Best ignore RemedialWork as its updating ok on a linq refresh.

Aug 6, 2010 at 9:03 PM
Edited Aug 6, 2010 at 9:05 PM

I got a hint from this question http://stackoverflow.com/questions/930350/how-to-refresh-a-wpf-datagrid

obList(0).RS_CallStatus = "WAIT"
OK, SO I now need to get the correct record from the observable collection, but I'm most of the way there.
Would actually be better if I could just rip the itemssource off the datagrid and put it back on. Maybe look into that.
Thanks for your help. Scott
Aug 8, 2010 at 11:31 AM

OK. The final part of this jigsaw (for anyone else in the same boat)

'Manual refresh of View.
        dbx.Refresh(System.Data.Objects.RefreshMode.ClientWins, dbx.View_EngineerManage)

 

I added this at the top of the code in the procedure PopulateListOfCalls in the first post. Cheers Scott