none
Navigating through and finding records in a CollectionView

    Question

  • Hello,

    I'm using the CollectionView to navigate through a datatable whose fields populate various parts of my WPF window.  The window is essentially a report or document showing all the fields from one record in the datatable.  If there is more than one record, I am enabling some record navigator buttons so that the user can move through the available reports.  I've added a SortDescription to the underlying CollectionViewSource so as to sort by report date.

    That all works very well.  The problem comes when the user wants to add a new report.  My Add button works fine, and adds a record to the underlying datatable, setting the report date to today.  As long there is no existing record with a DocumentDate of 'today', then MoveCurrentToFirst will display the newly added record.  But if there is already a record with a DocumentDate of 'today' then calling MoveCurrentToFirst may or may not show the new record.

    I've got around this by adding a SortDescription on the DocumentID field, in addition to the date.  I'm not sure what DocumentID the newly added record has prior to committing it to the database (perhaps Null?), but with a ListSortDirection of Ascending on the DocumentID, MoveCurrentToFirst does seem to move to the newly added record.

    It feels a bit of a kludge though so I'm wondering if there is a better way to 'find' the newly added record in the underlying datatable and then sync that with the CollectionView?  Or am I missing/ignorant of some basic aspects of the CollectionView?

    A second reason for wanting to 'find' a record within the CollectionView is that the user is able to choose a desired report, by date, using a separately populated combobox.  Once they have selected the desired date (assuming not more than one report per date), I now have the DocumentID via the SelectedValuePath.  I could perhaps use the Filter method on the CollectionView but it looks complicated to implement and I'm wondering if there's an easier way?

    So as to make it easier to see where I'm at now, herewith some code:

    1. Initial data population

        Private Sub GetCoPage2Data(ByVal BloombergCode As String)

            Try
                dsCoPage2Data = New CoPage2DataSet
                taCoPage2Data = New CoPage2DataSetTableAdapters.DocumentsTableAdapter

                taCoPage2Data.FillByBloombergCode(dsCoPage2Data.Documents, BloombergCode)

                If dsCoPage2Data.Documents.Rows.Count = 0 Then
                    AddNewCoPage2(True)
                End If

                docsCollection = New CollectionViewSource
                docsCollection.Source = dsCoPage2Data.Documents.DefaultView
                docsCollection.SortDescriptions.Add(New SortDescription("DocumentDate", ListSortDirection.Descending))
                docsCollection.SortDescriptions.Add(New SortDescription("DocumentID", ListSortDirection.Ascending))

                cvDocs = docsCollection.View
                cvDocs.MoveCurrentToFirst()
                Me.MainLayoutGrid.DataContext = cvDocs

                If cvDocs.Count > 1 Then Me.btnNext.IsEnabled = True

            Catch ex As Exception
                AddToLog("GetCoPage2Data", ex.Message, True)
                MessageBox.Show(ex.Message, "Error retrieving CoPage2Data")
            End Try

        End Sub

    2. Adding a new record

        Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnAdd.Click
            AddNewCoPage2(False)
        End Sub

        Private Sub AddNewCoPage2(ByVal IsFirstRecord As Boolean)
            Dim dr As CoPage2DataSet.DocumentsRow

            dr = dsCoPage2Data.Documents.NewDocumentsRow
            With dr
                .DocumentType = 1
                .DocumentDate = Today()
                .BloombergCode = m_Bloomberg
            End With

            dsCoPage2Data.Documents.Rows.Add(dr)

            If Not IsFirstRecord Then
                cvDocs.MoveCurrentToFirst()
                Me.btnPrevious.IsEnabled = False
                Me.btnNext.IsEnabled = True
            End If

        End Sub

    Many thanks for any guidance,

    Sebastian Crewe

    Wednesday, June 02, 2010 4:16 PM

Answers

  • Hi Sebastian,

    For your first question, since the newly added DataRow has the same datetime value as the current DataRow, the newly added DataRow won't the sorted to the first position. If you navigate the current item to next by calling cvDocs.MoveCurrentToNext method, you'll see the newly added DataRow.

    For your second question, I'm afraid there's no better solution than using filtering in the CollectionView.

    Anyway, filtering in CollectionView is not complicated. The following is an MSDN document which will help you:
    http://msdn.microsoft.com/en-us/library/system.windows.data.collectionview.filter.aspx

    Hope this helps.
    If you have any question, please feel free to let me know.

    Sincerely,
    Linda Liu

    MSDN Subscriber Support in Forum
    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Thursday, June 03, 2010 9:38 AM

All replies

  • Hi Sebastian,

    For your first question, since the newly added DataRow has the same datetime value as the current DataRow, the newly added DataRow won't the sorted to the first position. If you navigate the current item to next by calling cvDocs.MoveCurrentToNext method, you'll see the newly added DataRow.

    For your second question, I'm afraid there's no better solution than using filtering in the CollectionView.

    Anyway, filtering in CollectionView is not complicated. The following is an MSDN document which will help you:
    http://msdn.microsoft.com/en-us/library/system.windows.data.collectionview.filter.aspx

    Hope this helps.
    If you have any question, please feel free to let me know.

    Sincerely,
    Linda Liu

    MSDN Subscriber Support in Forum
    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Thursday, June 03, 2010 9:38 AM
  • Hi Sebastian,

    How about the problem now?

    If you have any question, please feel free to let me know.

    Sincerely,
    Linda Liu


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Monday, June 07, 2010 6:19 AM
  • Thanks so much.  As with a previous post last month, I'm not getting any alert when there is a response to my post despite having ticked the necessary box.  So I'm having to check in from time to time to see if there's been any activity.

    The link to the filtering example looks promising.  I'll give that a go when I'm back on that project.  And it is helpful to know I am not missing anything fundemental.

    With thanks and regards

    Sebastian

     

    Monday, June 07, 2010 6:54 PM