none
Data updated on one Winform fails to show up on another RRS feed

  • Question

  • LINQ to SQL, VS 2008, VB

     

    On one winform there are 2 comboboxes - one for State and one for County.  Based on user's selection, store address & related info is displayed in a datagridview.  Double-clicking a row in the dgv pops up a new form where the user can edit details on that store.  When finished, the user clicks on a save button and is returned to the original form.

     

    I have confirmed that the edited data has been passed through to SQL, but I am stumped on how to get the dgv to DISPLAY the updated data.

     

    Choosing a different state and/or county then returning to the original selections doesn't do it.

    Setting the dgv's datasource to Nothing then back to the LINQ query doesn't do it.

     

    CLOSING the app and re-opening DOES display the updated info.

     

    What must be done to display it without re-starting the app?

     

    Thanks

    Duke

     

     

    Tuesday, June 3, 2008 2:36 PM

Answers

  • Well, I'm such a rank novice that I missed the obvious step of telling the DataContext to refresh when the 2d form completes and returns program control to the first form.

     

            db.Refresh(Data.Linq.RefreshMode.KeepChanges)

     

    With that code added, all seems right with the world

    Tuesday, June 17, 2008 2:41 PM

All replies

  • How are you populating the combo box?

     

    Do both forms use the same DataContext?

     

    [)amien

     

    Monday, June 9, 2008 10:51 PM
    Moderator
  • 1) How are you populating the combo box?

     

    The State combobox gets populated in the form load event:

     

    Code Snippet

    Dim stateList = From states In db.Branches _

     Select states.St Distinct

     

    Me.StComboBox.DataSource = stateList

     

     

    When the user selects a State, the County combobox gets populated:

     

    Code Snippet

    Private Sub StComboBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StComboBox.SelectedIndexChanged

     

    Me.BranchesDataGridView.DataSource = Nothing

    Me.BranchesDataGridView.Refresh()

     

    Dim countyList = From counties In db.Branches _

      Where counties.St = Me.StComboBox.SelectedItem.ToString _

      Select counties.County Distinct

     

    Me.CountyComboBox.DataSource = countyList

     

    End Sub

     

     

    db is a public form level DataContext variable

     

    2) Do both forms use the same DataContext?

     

    Initially I was creating a new instance of the datacontext on the second form.  That led to the first form not reflecting the 2d form's update.  Since then I've tried 2 things with peculiar results:

    a) on returning from the 2d form, disposing of the 1st form's datacontext and creating a new instance,

    b) passing the first form's DataContext to the second form. 

    In both cases the 1st form reflects the changes made on the second form.  However, when the user NEXT double-clicks on a row on form 1, edits the row's details on form 2, and saves, triggering a call to DataContext.SubmitChanges(), the DataContext tries to delete every row in the SQL table.

     

    In trying to track this down, I've put this code in place of SubmitChanges:

    Code Snippet

    Dim cs As System.Data.Linq.ChangeSet = BMdb.GetChangeSet()

    Console.Write("Total changes: {0}", cs)

     

     

    The first time through the console shows this:  Total changes: {Inserts: 0, Deletes: 0, Updates: 1}

    The second attempt at update shows this: Total changes: {Inserts: 0, Deletes: 617, Updates: 0}

    There isn't anything fancy going on in this code - just a simple filter the DataGridView rows by the user's selected State and County, and edit a single, selected row.
    Tuesday, June 17, 2008 11:28 AM
  • One other bit of info:

     

    From the first form, when the user double-clicks a row, the form passes the store's ID to the 2d form.  The 2d form executes a LINQ query to load the details associated with that store ID into the text boxes for editing.

     

    This seems like the easiest and most straightforward approach, rather than passing an array of detail values to the second form and then returning the edited data to the first form for processing.

     

    Is this approach valid?

     

    Tuesday, June 17, 2008 11:41 AM
  • Well, I'm such a rank novice that I missed the obvious step of telling the DataContext to refresh when the 2d form completes and returns program control to the first form.

     

            db.Refresh(Data.Linq.RefreshMode.KeepChanges)

     

    With that code added, all seems right with the world

    Tuesday, June 17, 2008 2:41 PM