title: C# save changes in datagridview
body: <p>I have DataGridView when I press to modify button then make same changes and Save it after refresh there are values same like before changes.</p>
  
  <p>button 1 - insert new record
  button 2 - refresh DataGridView
  button 3 - allow to update records in DataGridView
  button 4 - save changes
  button 5 - cancel changes</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.ComponentModel;
  using System.Data;
  using System.Drawing;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  using System.Windows.Forms;
  using System.Data.SqlClient;
  using System.Configuration;
  
  namespace IS
  {
  public partial class Form2 : Form
  {
      public Form2()
      {
          InitializeComponent();
          LoadTable();
  
          label1.Text = "User: " + User.name + " " + User.surename;
      }
  
      private void label1_Click(object sender, EventArgs e)
      {
  
      }
  
      private void LoadTable()
      {
          using (SqlConnection connection = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\***\Documents\Visual Studio 2015\Projects\***\Database1.mdf;Integrated Security=True;Connect Timeout=30"))
          using (SqlCommand query = new SqlCommand("Select FirstName, LastName, Login, Email from Users", connection))
          {
              try
              {
                  SqlDataAdapter Adapter = new SqlDataAdapter();
                  Adapter.SelectCommand = query;
  
                  DataTable UsersTable = new DataTable();
                  Adapter.Fill(UsersTable);
                  BindingSource UserTableSource = new BindingSource();
  
                  UserTableSource.DataSource = UsersTable;
                  dataGridView1.DataSource = UserTableSource;
                  Adapter.Update(UsersTable);
              }
              catch(Exception ex)
              {
                  MessageBox.Show(ex.Message);
              }
          }
      }
      private void label1_Click_1(object sender, EventArgs e)
      {
  
      }
  
      private void UserManageToolStripMenuItem_Click(object sender, EventArgs e)
      {
          panel2.Visible = true;
      }
  
      private void button1_Click(object sender, EventArgs e)
      {
          Form3 form3 = new Form3();
          form3.FormClosing += new FormClosingEventHandler(this.Form3_FormClosing);
          form3.Show();
      }
  
      private void button2_Click(object sender, EventArgs e)
      {
          LoadTable();
      }
  
      private void Form3_FormClosing(object sender, FormClosingEventArgs e)
      {
          LoadTable();
      }
  
      private void button3_Click(object sender, EventArgs e)
      {
          dataGridView1.ReadOnly = false;
          button4.Visible = true;
          button5.Visible = true;
      }
  
      private void button4_Click(object sender, EventArgs e)
      {
          try
          {
              SqlDataAdapter sda = new SqlDataAdapter();
              SqlCommandBuilder scb = new SqlCommandBuilder();
              DataTable dt = new DataTable();
  
              scb = new SqlCommandBuilder(sda);
              sda.Update(dt);
  
              dataGridView1.ReadOnly = true;
              button4.Visible = false;
              button5.Visible = false;
          }
          catch(Exception ex)
          {
              MessageBox.Show(ex.Message);
          }
      }
  
      private void button5_Click(object sender, EventArgs e)
      {
          LoadTable();
          button4.Visible = false;
          button5.Visible = false;
      }
  }
  }
  </code></pre>