Link to home
Start Free TrialLog in
Avatar of Hagita
HagitaFlag for Israel

asked on

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
===============================================================
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

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.
Avatar of Hagita

ASKER

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
You mean if you add a thread.sleep between update and reload, it works?
Avatar of Hagita

ASKER

I changed the Messagbox "UPDATED" timing to be before the refresh code and now it works...
Can you show the code you have now?
Avatar of Hagita

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Hagita

ASKER

Yes. You are right. its more elegant and it works.Thanks!
Glad to help :-)