vb.net datagrid not showing database changes when updated from second form

I am running vb.net 2010 (Visual Studio).  I have 2 forms, Form1 and Form2.

Form1 shows a datagrid.  User may double click on a row in the grid to load Form2 where maintenance can be performed on the selected item.

Originally, I had run Form2.ShowDialog, followed immediately by sbrReset from Form1 when user double clicks row.  This worked fine, changes made in database were reflected in grid because entire grid reloaded via sbrReset.

However, it is necessary that I not use Form2.ShowDialog, but must use Form2.Show.  By doing this I cannot run sbrReset after Form2.Show because the changes have not been made to the database until Form2 is completed.

I have attempted to resolve the issue by placing a hidden text box on Form1, and having Form2 change the text value to “1” upon exiting.  Then I coded the hidden text box textchanged event, which should reload the grid in Form1.  Unfortunately, when the grid reloads the database changes are not reflected.

Here is what I know via debugging.
When I save the changes on Form2, the database has been changed, verified via SQL query.
When Form2 closes, Form1 hidden text box textchanged event fires, which runs sbrReset.
A breakpoint on Form1 sbrReset  bsGrid1.DataMember = "Table" line shows the Table does have the database changes, but those changes are NOT reflected in the grid on Form1.

I have put a menu option on Form1 to reload the grid, and after changes made on Form2 and control back on Form1, if I click the menu option the grid is reloaded with the correct database changes.

I hope I am missing something simple, but have tried every change I can think of and cannot get it to work.  I need to be able to reload the grid (with correct info) upon completion of Form2.  I would certainly appreciate any assistance.


    '****************************************************************
    ' Form 1 code
    '****************************************************************
Public Class frmForm1
    Dim dsGrid1 As New DataSet
    Dim daGrid1 As New SqlDataAdapter
    Dim bsGrid1 As New BindingSource

    '****************************************************************
    'Form1 Form initialization
    '****************************************************************
    Private Sub frmForm1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Me.Load
        sbrReset()
    End Sub

    '****************************************************************
    ' Form 1 load or reload grid
    '****************************************************************
    Private Sub sbrReset()
        Try
            GridControl1.DataSource = Nothing
            GridView1.Columns.Clear()
            dsGrid1.Clear()


            Using cmd As New SqlClient.SqlCommand("spInventoryControlMtnFiberSize", GetOpenConnection) With {.CommandType = CommandType.StoredProcedure}
                With cmd.Parameters
                    .Add(New SqlParameter("@Action", SqlDbType.Int)).Value = 23
                End With

                daGrid1.SelectCommand = cmd
                daGrid1.Fill(dsGrid1, "Table")
                bsGrid1.DataSource = dsGrid1
                bsGrid1.DataMember = "Table"
            End Using

            GridControl1.DataSource = bsGrid1

            GridView1.BestFitColumns()
            GridView1.Columns(0).Fixed = Columns.FixedStyle.Left
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub


    '****************************************************************
    ' Form1 when user double clicks grid row,show maintenance form
    '****************************************************************
    Private Sub GridView1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GridView1.DoubleClick
        glbFiberSizeCode_int = CInt(GridView1.GetRowCellValue(GridView1.FocusedRowHandle, GridView1.Columns(0)).ToString)
        Dim xfrmForm2 As New frmForm2
        xfrmForm2.Show()
    End Sub


    '****************************************************************
    ' Form1 process text box text changed (done via Form2)
    '****************************************************************
    Private Sub txtBoxFiberSizeChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles _
      txtFiberSizeChanged.TextChanged
        If txtFiberSizeChanged.Text = "1" Then
            sbrReset()
            txtFiberSizeChanged.Text = "0"
        End If
    End Sub


    '****************************************************************
    ' Form1 menu option to reload grid
    '****************************************************************
    Private Sub mnuReload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuReload.Click
        sbrReset()
    End Sub



    '****************************************************************
    ' From2  coding, exit when changed completed
    '****************************************************************
    Private Sub sbrExit()
        Me.Close()
        frmMtnFiberSize01.txtFiberSizeChanged.Text = "1"
    End Sub


    '****************************************************************
    ' Form2  dispose of Form2
    '****************************************************************
    Private Sub frmMtnFiberSize02_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        Me.Dispose()
    End Sub
jhinsonAsked:
Who is Participating?
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.

it_saigeDeveloperCommented:
The problem stems from the fact that you did not instruct the binding source to reset.  You can do this with a few different methods, but I personally prefer the CurrencyManager method (something like):
Private Sub sbrReset()
	Try
		GridControl1.DataSource = Nothing
		GridView1.Columns.Clear()
		dsGrid1.Clear()

		Using cmd As New SqlClient.SqlCommand("spInventoryControlMtnFiberSize", GetOpenConnection) With {.CommandType = CommandType.StoredProcedure}
			With cmd.Parameters
				.Add(New SqlParameter("@Action", SqlDbType.Int)).Value = 23
			End With

			daGrid1.SelectCommand = cmd
			daGrid1.Fill(dsGrid1, "Table")
			bsGrid1.DataSource = dsGrid1
			bsGrid1.DataMember = "Table"
		End Using

		GridControl1.DataSource = bsGrid1
		Dim cm As CurrencyManager = DirectCast(BindingContext(GridControl1.DataSource), CurrencyManager)
		cm.Refresh()

		GridView1.BestFitColumns()
		GridView1.Columns(0).Fixed = Columns.FixedStyle.Left
	Catch ex As Exception
		MsgBox(ex.ToString)
	End Try
End Sub

Open in new window

-saige-
0
jhinsonAuthor Commented:
Had no effect.
0
it_saigeDeveloperCommented:
Let me throw something together to see if I can replicate and correct your issue.

-saige-
0
jhinsonAuthor Commented:
I changed my approach to have frmForm1 process an event when frmForm2 closed.  This resolved the issue.

** Added the handler for frmForm2
        Dim xfrmForm2 As New frmForm2
        AddHandler xfrmForm2.FormClosed, AddressOf xfrmChildFormClosed
        xfrmForm2.Show()

** Process frmForm2 closed
    Sub xfrmChildFormClosed(ByVal sender As Object, ByVal e As FormClosedEventArgs)
        sbrReset()
        Dim xfrmForm2 As New frmForm2
        RemoveHandler xfrmForm2.FormClosed, AddressOf xfrmChildFormClosed
    End Sub


Will moderators please close this question.
0

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
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
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.