title: How to reload datagridview in vbnet with sql server database
body: <p>I'm still learning in programming from ebook and Internet. Currently, I'm interested in creating database application using vb.net for the interface and Sql Server for database.<br></p>
  
  <p>My project is simple add, save, edit, delete records in database.<br></p>
  
  <p>Here is my code <br></p>
  
  <pre class="lang-vb prettyprint-override"><code>Imports System.Data
  Imports System.Data.SqlClient
  
  Public Class FormCabinet
  
      'Variable Declaration
      Dim CabinetCommand As SqlCommand
      Dim CabinetAdapter As SqlDataAdapter
      Dim CabinetTable As DataTable
      Dim CabinetManager As CurrencyManager
      Dim MyState As String, MyBookmark As Integer
  
      Private Sub FormCabinet_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  
          Try
              Call OPENCONN()
  
              CabinetCommand = New SqlCommand("SELECT * FROM [CABINET]", CONN)
              CabinetAdapter = New SqlDataAdapter()
              CabinetAdapter.SelectCommand = CabinetCommand
              CabinetTable = New DataTable()
              CabinetAdapter.Fill(CabinetTable)
  
              'bind control to data table
              TbCabID.DataBindings.Add("Text", CabinetTable, "CABINET_ID")
              TbCabinet.DataBindings.Add("Text", CabinetTable, "CAB_TAG")
              TbDescription.DataBindings.Add("Text", CabinetTable, "CAB_DESC")
  
              'establish currency manager
              CabinetManager = DirectCast(Me.BindingContext(CabinetTable), CurrencyManager)
          Catch ex As Exception
              MessageBox.Show(ex.Message, "Error establishing Cabinet Table", MessageBoxButtons.OK, MessageBoxIcon.Error)
              Exit Sub
          End Try
          Me.Show()
  
          Call SetState("View")
  
      End Sub
  
      Private Sub FormCabinet_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.Closing
          If MyState = "Edit" Or MyState = "Add" Then
              MessageBox.Show("You must finish current edit before closing the form", "", MessageBoxButtons.OK, MessageBoxIcon.Information)
              'exit cancel procedure
              e.Cancel = True
          Else
              Try
                  'save changes to database
                  Dim CabinetAdapterCommands As New SqlCommandBuilder(CabinetAdapter)
                  CabinetAdapter.Update(CabinetTable)
              Catch ex As Exception
                  MessageBox.Show("Error saving database to file:" + ControlChars.CrLf + ex.Message, "Save Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
              End Try
              'close connection
              CONN.Close()
              'dispose of objects
              CabinetCommand.Dispose()
              CabinetAdapter.Dispose()
              CabinetTable.Dispose()
          End If
      End Sub
  
      'Sub setState
      Private Sub SetState(ByVal AppState As String)
          MyState = AppState
          Select Case AppState
              Case "View"
                  BtnFirst.Enabled = True
                  BtnPrevious.Enabled = True
                  BtnNext.Enabled = True
                  BtnLast.Enabled = True
                  BtnEdit.Enabled = True
                  BtnSave.Enabled = False
                  BtnCancel.Enabled = False
                  BtnAdd.Enabled = True
                  TbCabID.ReadOnly = True
                  TbCabinet.ReadOnly = True
                  TbDescription.ReadOnly = True
              Case "Add", "Edit"
                  BtnFirst.Enabled = False
                  BtnPrevious.Enabled = False
                  BtnNext.Enabled = False
                  BtnLast.Enabled = False
                  BtnEdit.Enabled = False
                  BtnSave.Enabled = True
                  BtnCancel.Enabled = True
                  BtnAdd.Enabled = False
                  TbCabID.ReadOnly = False
                  TbCabinet.ReadOnly = False
                  TbDescription.ReadOnly = False
          End Select
  
      End Sub
  
      'Input Validation
      Private Function ValidateData() As Boolean
          Dim Message As String = ""
          Dim AllOK As Boolean = True
          'Check for name 
          If TbCabinet.Text.Trim = "" Then
              Message = "You must enter an Cabinet Name." + ControlChars.CrLf
              TbCabinet.Focus()
              AllOK = False
          End If
          If Not (AllOK) Then
              MessageBox.Show(Message, "Validation Error", MessageBoxButtons.OK, MessageBoxIcon.Information)
          End If
          Return (AllOK)
      End Function
  
      Private Sub BtnAdd_Click(sender As Object, e As EventArgs) Handles BtnAdd.Click
          Try
              MyBookmark = CabinetManager.Position
              CabinetManager.AddNew()
              Call SetState("Add")
          Catch ex As Exception
              MessageBox.Show("Error adding record.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
          End Try
      End Sub
  
      Private Sub BtnDelete_Click(sender As Object, e As EventArgs) Handles BtnDelete.Click
          Dim Response As Windows.Forms.DialogResult
          Response = MessageBox.Show("Are you sure you want to delete this record ?", "Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2)
          'show dialogue
          If Response = Windows.Forms.DialogResult.No Then
              Exit Sub
          End If
          'if yes then delete at current position
          Try
              CabinetManager.RemoveAt(CabinetManager.Position)
          Catch ex As Exception
              MessageBox.Show("Error deleting record.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
          End Try
      End Sub
  
      'Button Cancel
      Private Sub BtnCancel_Click(sender As Object, e As EventArgs) Handles BtnCancel.Click
          CabinetManager.CancelCurrentEdit()
          If MyState = "Add" Then
              CabinetManager.Position = MyBookmark
          End If
          Call SetState("View")
      End Sub
  
      Private Sub BtnEdit_Click(sender As Object, e As EventArgs) Handles BtnEdit.Click
          Call SetState("Edit")
      End Sub
  
      'Button Save
      Private Sub BtnSave_Click(sender As Object, e As EventArgs) Handles BtnSave.Click
          If Not (ValidateData()) Then Exit Sub
          Dim SavedName As String = TbCabID.Text
          Dim SavedRow As Integer
          Try
              CabinetManager.EndCurrentEdit()
              CabinetTable.DefaultView.Sort = "CABINET_ID"
              SavedRow = CabinetTable.DefaultView.Find(SavedName)
              CabinetManager.Position = SavedRow
              MessageBox.Show("Record Saved", "Save", MessageBoxButtons.OK, MessageBoxIcon.Information)
              Call SetState("View")
  
  
          Catch ex As Exception
              MessageBox.Show("Error saving record.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
          End Try
      End Sub
  End Class
  </code></pre>
  
  <p>I'm trying to add DataGridView in my Form. However, after I add a DataGridView using this subroutine and call it in my FormCabinet_Load, I can't save the records again. </p>
  
  <pre class="lang-vb prettyprint-override"><code>Sub ShowGrid()
  
      Call OPENCONN()
      CabinetCommand = New SqlCommand("SELECT [CABINET_ID] AS 'CABINET ID', [CAB_TAG] AS 'TAG NUMBER', [CAB_DESC] AS 'DESCRIPTION' FROM [CABINET] ORDER BY CABINET_ID", CONN)
      CabinetAdapter = New SqlDataAdapter()
      CabinetAdapter.SelectCommand = CabinetCommand
      CabinetTable = New DataTable()
      CabinetAdapter.Fill(CabinetTable)   
      DataGridView1.DataSource = CabinetTable
  
  End Sub
  </code></pre>
  
  <p>I have tried to call <code>ShowGrid()</code> in the end of the <code>BtnSave_click</code> subrotine, and <br></p>
  
  <pre class="lang-vb prettyprint-override"><code>CabinetAdapter.Update(CabinetTable) 
  </code></pre>
  
  <p>in order to update the database right away. However it only works one time. I need to close the Form and start it from the beginning to edit my database.<br></p>
  
  <p>How to reload/refresh the DataGridView?<br></p>
  
  <p>Any advice or link is appreciated<br>
  Kindly Please let me know if you need further information</p>