title: How to Refresh. Datagridview after adding data.im using sql database
body: <p>below is my code for my project.
  hope you can help me.
  thanks in advance. :)</p>
  
  <p>this is my sqlcontrol code</p>
  
  <pre><code>Imports System.Data
  Imports System.Data.SqlClient
  
  Public Class SQLControl
  
      Public SQLCon As New SqlConnection With {.ConnectionString = "Server=xxx\SQLEXPRESS;Database=SQLTest;User=sa;Pwd=xxxx;"}
      Public SQLCmd As SqlCommand 'allow us to fire query at the data base
      Public SQLDA As SqlDataAdapter
      Public SQLDataset As DataSet
      Public dtable As New DataTable
      Public bs As New BindingSource
      'QUERY PARAMETERS
      Public Params As New List(Of SqlParameter)
  
      Public RecordCount As Integer
      Public Exception As String
  
      Public Function HasConnection() As Boolean
          Try
              SQLCon.Open()
              SQLCon.Close()
              Return True
          Catch ex As Exception
              MsgBox(ex.Message)
              Return False
          End Try
      End Function
  
      Public Sub ExecQuery(ByVal Query As String)
          Try
              SQLCon.Open()
  
              'CREATE SQL COMMAND
              SQLCmd = New SqlCommand(Query, SQLCon)
  
              'LOAD PARAMETER INTO SQL COMMAND
              Params.ForEach(Sub(x) SQLCmd.Parameters.Add(x))
  
              'CLEAR PARAMETER LIST
              Params.Clear()
  
              'EXCUTE COMMAND FILL MY DATASET
              SQLDataset = New DataSet
              'EXCUTE COMMAND WIHTOUT ADAPTER 
              SQLDA = New SqlDataAdapter(SQLCmd)
              RecordCount = SQLDA.Fill(SQLDataset)
  
              SQLCon.Close()
          Catch ex As Exception
              Exception = ex.Message
          End Try
  
          If SQLCon.State = ConnectionState.Open Then SQLCon.Close()
  
      End Sub
  
      Public Sub RunQuery(ByVal Query As String)
          Try
              SQLCon.Open()
  
              SQLCmd = New SqlCommand(Query, SQLCon)
  
              'LOAD SQL RECORDS FOR DATAGRID
              SQLDA = New SqlDataAdapter(SQLCmd)
              SQLDataset = New DataSet
              SQLDA.Fill(SQLDataset)
  
              SQLCon.Close()
          Catch ex As Exception
              MsgBox(ex.Message)
  
              If SQLCon.State = ConnectionState.Open Then
                  SQLCon.Close()
              End If
          End Try
      End Sub
  
      Public Sub AddMember(ByVal PC As String, ByVal IP As String, ByVal Name As String, ByVal Email As String, ByVal Department As String, ByVal Location As String,
                       ByVal Model As String, ByVal Specs As String, ByVal Dt As String, ByVal Asset As String, ByVal Rent As String)
  
          Try
              Dim strInsert As String = "INSERT INTO MEMBERS (pc,ip,name,email,department,location,model,specs,date,asset,rent) " &amp; _
                                   "VALUES (" &amp; _
                                   "'" &amp; PC &amp; "'," &amp; _
                                   "'" &amp; IP &amp; "'," &amp; _
                                   "'" &amp; Name &amp; "'," &amp; _
                                   "'" &amp; Email &amp; "'," &amp; _
                                   "'" &amp; Department &amp; "'," &amp; _
                                   "'" &amp; Location &amp; "'," &amp; _
                                   "'" &amp; Model &amp; "'," &amp; _
                                   "'" &amp; Specs &amp; "'," &amp; _
                                   "'" &amp; Dt &amp; "'," &amp; _
                                   "'" &amp; Asset &amp; "'," &amp; _
                                   "'" &amp; Rent &amp; "')"
              MsgBox(strInsert)
  
              SQLCon.Open()
              SQLCmd = New SqlCommand(strInsert, SQLCon)
  
              SQLCmd.ExecuteNonQuery()
              SQLCon.Close()
          Catch ex As Exception
              MsgBox(ex.Message)
          End Try
      End Sub
  End Class
  </code></pre>
  
  <p>And this is my form code:</p>
  
  <pre><code>Public Class Form1
      Private SQL As New SQLControl
  
      Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
          'EXECUTE QUERY AND POPULATE GRID
          SQL.ExecQuery("SELECT * FROM members")
          LoadGrid()
  
          'DISABLE SAVE BUTTON
          cmdSave.Enabled = False
      End Sub
  
      Private Sub LoadGrid()
          'IF OUR DATA IS RETURNED AND POPULATE GRID &amp; BUILD UPDATE COMMAND
          If SQL.RecordCount &gt; 0 Then
              dgvData.DataSource = SQL.SQLDataset.Tables(0)
              dgvData.Rows(0).Selected = True
              SQL.SQLDA.UpdateCommand = New SqlClient.SqlCommandBuilder(SQL.SQLDA).GetUpdateCommand
          End If
      End Sub
  
      Private Sub dgvData_CellValueChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvData.CellValueChanged
          cmdSave.Enabled = True
  
      End Sub
  
      Private Sub dgvData_RowsRemoved(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowsRemovedEventArgs) Handles dgvData.RowsRemoved
          cmdSave.Enabled = True
      End Sub
  
      Private Sub cmdSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSave.Click
          'SAVE UPDATE TO THE DATA BASE
          Try
              SQL.SQLDA.Update(SQL.SQLDataset) ' TO DO: DATA VALIDATION
          Catch ex As Exception
              MsgBox("Already Exists")
          End Try
  
          'REFRESH GRID DATA
          LoadGrid()
  
          'DISABLE SAVE BUTTON
          cmdSave.Enabled = False
  
      End Sub
  
      Private Sub cmdAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAdd.Click
  
          If Trim(txtPC.Text) = "" Then
              MsgBox("Please fill out the pc name.")
              Exit Sub
          End If
  
          If Trim(txtIP.Text) = "" Then
              MsgBox("Please fill out the ip address.")
              Exit Sub
          End If
  
          'Query for user
          SQL.RunQuery("SELECT * FROM members WHERE members.PC = '" &amp; txtPC.Text &amp; "'")
  
          If SQL.SQLDataset.Tables(0).Rows.Count &gt; 0 Then
              MsgBox("The name that you have enter enter is already exists")
              Exit Sub
          End If
          SQL.RunQuery("SELECT * FROM members WHERE members.IP = '" &amp; txtIP.Text &amp; "'")
          If SQL.SQLDataset.Tables(0).Rows.Count &gt; 0 Then
              MsgBox("The IP Address that you have enter is already exists!")
              Exit Sub
          End If
  
          SQL.RunQuery("SELECT * FROM members WHERE members.Asset = '" &amp; txtAsset.Text &amp; "'")
          If SQL.SQLDataset.Tables(0).Rows.Count &gt; 0 Then
              MsgBox("The Asset that you have enter is already exists")
              Exit Sub
          Else
  
              CreateUser()
              txtPC.Clear()
              txtIP.Clear()
              txtName.Clear()
              txtEmail.Clear()
              txtDepartment.Clear()
              txtLocation.Clear()
              txtModel.Clear()
              txtSpecs.Clear()
              txtDt.Clear()
              txtAsset.Clear()
              txtRent.Clear()
          End If
      End Sub
  
      Public Sub CreateUser()
          SQL.AddMember(txtPC.Text, txtIP.Text, txtName.Text, txtEmail.Text, txtDepartment.Text,
                    txtLocation.Text, txtModel.Text, txtSpecs.Text, txtDt.Text, txtAsset.Text, txtRent.Text)
      End Sub
  
  End Class
  </code></pre>
  
  <p>I don't know how to refresh the <code>datagridview</code></p>