title: How to refresh the data grid view when update? Window from C#
body: <p>I am trying to refresh the data grid view after the user insert some item to the database. And I tried to null the data source of the view and apply a new data source to it, but it didn't work.</p>
  
  <p>Here is the flow of the system:</p>
  
  <ol>
  <li>Click the add Item, then pop-up a <code>add-item form</code></li>
  <li>Input the product detail, then click the button to insert data</li>
  <li>View update &lt;-----Problem</li>
  </ol>
  
  <p><a href="https://i.stack.imgur.com/Lt2dZ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Lt2dZ.png" alt="enter image description here"></a></p>
  
  <p>In this part, there will have <code>two forms</code> and one <code>database claee</code>. They are <code>Form1</code> (the data view), <code>Add-item form</code> and the <code>database_function.cs</code>.</p>
  
  <p>Here is my code on <code>database class</code>:</p>
  
  <pre><code>  public class database_function
  {
     OleDbConnection connect = new OleDbConnection();
  
  
      public database_function()
      {
          connect.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\sys_db.accdb";
      }
  
      public void database_connect(String item_code, String des, String unit, double price)
      {
        Form1 f1 = new Form1();
          try
          {
  
              connect.Open();
              OleDbCommand command = new OleDbCommand();
              command.Connection = connect;
              command.CommandText = "INSERT into item (item_code, description, unit, price) values ('" + item_code + "', '" + des + "', '" + unit + "', " + price + ")";
              command.ExecuteNonQuery();
  
              connect.Close();
          }
          catch(Exception e)
          {
              Debug.WriteLine(e.Source);
              connect.Close();
          }
         f1.refresh_dataGridView();
      }
  
  
  
      //return the dataGridView to form 1, and show the database data on it.
      public DataTable get_view()
      {
          connect.Open();
          OleDbCommand command = new OleDbCommand();
          command.Connection = connect;
          string query = "Select item_code, description, unit, price from item";
          command.CommandText = query;
  
          OleDbDataAdapter da = new OleDbDataAdapter(command);
          DataTable dt = new DataTable();
          da.Fill(dt);
          connect.Close();
          Debug.WriteLine("return dataTable");
          return dt;
      }
  }
  </code></pre>
  
  <p>Here is the code on <code>form1</code>:
         //Get the data source when the form1 is loading
         private void Form1_Load(object sender, EventArgs e)
          {
              dataGridView1.DataSource = df.get_view();<br>
          }</p>
  
  <pre><code>   //Refresh the view
     public void refresh_dataGridView()
      {
          dataGridView1.DataSource = null;
          dataGridView1.DataSource = df.get_view();
          Debug.WriteLine("refuesh");
      }
  </code></pre>
  
  <p>Here is the <code>add-item. form</code>:</p>
  
  <pre><code>private void button1_Click(object sender, EventArgs e)
      {
         //Get textfield data....
  
          //If user doesn't input the item data, show the error message. Else, update to database.
          if (error == true)
          {
              error_msg_form emf = new error_msg_form();
              emf.Show();
          }
          else
          {
              Form1 f1 = new Form1();
              database_function df = new database_function();
               df.database_connect(item_code_tb.Text, des_tb.Text, unit_tb.Text, Convert.ToDouble(unit_price_tb.Text));
              //f1.refresh_dataGridView();
          }
      }
  </code></pre>
  
  <p>However, I debug log will show the <code>refresh</code>, that mean it can run in the <code>refresh_dataGridView()</code>. But it cannot refresh the view.</p>
  
  <p>What is the problem is it? Or there have another way to do this? 
  Thanks</p>