Data Grid View Refresh Problem after Update Table

Hello Experts, need a big and fast help here Please...
I use a datagridview in a vb.net project to show data from a disconnected dataset. when the user update the grid data I use this code to update table and refresh the grid's data. problem is the data wont refresh even though the update table has been done:
======================================================
  If objBs.UpdateCrossSection(dtRow, NEW_ROW) Then
                NEW_ROW = False
                EDIT_MODE = False
                dtgCon.Update()
                'DTV = dtgCon.DataSource
                DT = DTV.Table
                If objBs.UpdateTable(DT, "frmCrossSection") Then

                    dtgCon.DataSource = Nothing
                    If Not IsNothing(DTV) Then DTV = Nothing
                    If Not IsNothing(dtsCR.Tables("tblConstruction")) Then dtsCR.Tables("tblConstruction").Clear()
                    If objBs.GetTable(dtsCR, "tblConstruction", "SELECT * FROM tblConstruction WHERE LabRepId=" & _
                          lngLabRepId & " ORDER BY COUNTER ASC") Then
                        DTV = dtsCR.Tables("tblConstruction").DefaultView
                        dtgCon.DataSource = DTV
                        DrawGrid()
                    End If
===============================================================
HagitaAsked:
Who is Participating?
 
CodeCruiserConnect With a Mentor Commented:
I think Thread.Sleep will achieve the same thing. Give that a try as well

System.Threading.Thread.Sleep(500)
0
 
CodeCruiserCommented:
And you are sure that the DB is being updated? And you are sure that

 If objBs.UpdateTable(DT, "frmCrossSection") Then


is returning true? Step through the code to see what's going on.
0
 
HagitaAuthor Commented:
Yes I know that it is updated. I solved it by timing and waiting few seconds : it refreshes well now. still dont understand why it happens
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
CodeCruiserCommented:
You mean if you add a thread.sleep between update and reload, it works?
0
 
HagitaAuthor Commented:
I changed the Messagbox "UPDATED" timing to be before the refresh code and now it works...
0
 
CodeCruiserCommented:
Can you show the code you have now?
0
 
HagitaAuthor Commented:
Sure I can. also notice I call the txtRepNo_Validating sub to do the refresh but its the same code I used earlier. the MsgBox changes everything:

     If objBs.UpdateCrossSection(dtRow, NEW_ROW) Then
                NEW_ROW = False
                EDIT_MODE = False
                dtgCon.Update()
                DTV = dtgCon.DataSource
                DT = DTV.Table
                If objBs.UpdateTable(DT, "frmCrossSection") Then
                    MsgBox("Update Success", vbInformation)
                    Dim e As System.ComponentModel.CancelEventArgs
                    txtRepNo_Validating(Me.btnSave, e)
                End If
            End If
======================
 Private Sub txtRepNo_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtRepNo.Validating
        Try
            If txtRepNo.Text <> "" Then
                lngLabRepId = CLng(txtRepNo.Text)
                If IsNewLabRepId(lngLabRepId) Then 'Is New Row
                    Me.Text = "Add New Row - Cross Section Report"
                    Dim dtDate As Date
                    dtDate = Today & " " & TimeOfDay
                    txtEltekPN.Text = ""
                    cmbLotNo.Text = ""
                    CuDepoTextBox.Text = ""
                    txtNote.Text = ""
                    CracksCheckBox.Checked = False
                    RegistrationCheckBox.Checked = False
                    RougnessCheckBox.Checked = False
                    VoidsCheckBox.Checked = False
                    dtTestdate.Value = dtDate

                    NEW_ROW = True
                    If Not IsNothing(dtsCR.Tables("tblConstruction")) Then dtsCR.Tables("tblConstruction").Clear()
                    If objBs.GetTable(dtsCR, "tblConstruction", "SELECT * FROM tblConstruction WHERE LabRepId=" & _
                             lngLabRepId & " ORDER BY COUNTER ASC") Then
                        DTV = dtsCR.Tables("tblConstruction").DefaultView
                        dtgCon.DataSource = DTV
                        DrawGrid()
                    End If
                              Else
                    Me.Text = "Update Row - Cross Section Report"
                    If Not IsNothing(dtsCR.Tables("CS")) Then dtsCR.Tables("CS").Clear()
                    If objBs.GetCs(dtsCR, lngLabRepId) Then
                        If dtsCR.Tables("CS").Rows.Count > 0 Then
                            lngLabRepId = CLng(dtsCR.Tables("CS").Rows(0)("LabRepID"))
                            If lngLabRepId > 0 Then
                                NEW_ROW = False
                            Else
                                NEW_ROW = True
                            End If
                            txtRepNo.Text = dtsCR.Tables("CS").Rows(0)("LabRepID").ToString
                            txtEltekPN.Text = dtsCR.Tables("CS").Rows(0)("EltekPN").ToString
                            cmbLotNo.Text = dtsCR.Tables("CS").Rows(0)("WorkingNO").ToString
                            CuDepoTextBox.Text = dtsCR.Tables("CS").Rows(0)("CuDepo").ToString
                            txtNote.Text = dtsCR.Tables("CS").Rows(0)("Notes").ToString
                            CracksCheckBox.Checked = dtsCR.Tables("CS").Rows(0)("Cracks")
                            RegistrationCheckBox.Checked = dtsCR.Tables("CS").Rows(0)("Registration")
                            RougnessCheckBox.Checked = dtsCR.Tables("CS").Rows(0)("Rougness")
                            VoidsCheckBox.Checked = dtsCR.Tables("CS").Rows(0)("Voids")
                            dtTestdate.Value = CDate(dtsCR.Tables("CS").Rows(0)("datee"))
                            txtTester.Text = dtsCR.Tables("CS").Rows(0)("TesterName").ToString
                        End If
                    End If
                    If Not IsNothing(dtsCR.Tables("tblConstruction")) Then dtsCR.Tables("tblConstruction").Clear()
                    If objBs.GetTable(dtsCR, "tblConstruction", "SELECT * FROM tblConstruction WHERE LabRepId=" & _
                             lngLabRepId & " ORDER BY COUNTER ASC") Then
                        DTV = dtsCR.Tables("tblConstruction").DefaultView
                        dtgCon.DataSource = DTV
                        DrawGrid()
                    End If
                          End If
            End If
        Catch ex As Exception
            MessageBox.Show("Error occured: " & ex.Message, "Error.", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub
0
 
HagitaAuthor Commented:
Yes. You are right. its more elegant and it works.Thanks!
0
 
CodeCruiserCommented:
Glad to help :-)
0
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.

All Courses

From novice to tech pro — start learning today.