title: Use one button for both save and edit on Gridview in c#
body: <p>I have a scenario where I can save the name and phone number. but now I want to use my "Save" button as update button from clicking on grid view data.</p>
  
  <p>Also after clicking on gridview data "save" button automatically change as "Update"
  I want to know if there is any event or something to make it. </p>
  
  <p>Screenshot of my form:</p>
  
  <p><img src="https://i.stack.imgur.com/1vmb6.png" alt="enter image description here"></p>
  
  <p>Here is my code:</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;
  
  namespace StoredProcedureExample
  {
      public partial class Form1 : Form
      {
          SqlConnection conn = new SqlConnection("Data Source=DESKTOP-S80LS9D;Initial Catalog=ComboBoxTest;Integrated Security=True");
          SqlCommand cmd;
          SqlDataAdapter data;
          DataSet ds;
          int num = 0;
          int i;
  
          public Form1()
          {
              InitializeComponent();
              cmd = new SqlCommand("UserInformation", conn);
              data = new SqlDataAdapter(cmd);
              ds = new DataSet();
              data.Fill(ds);
              this.userdataGridView1.DataSource = ds.Tables[0];
          }
  
          private void saveButton_Click(object sender, EventArgs e)
          {
              SaveRecord();
              ShowRecordAfterSaveAndEdit();
              clearfilds();
          }
  
          public void SaveRecord()
          {
              cmd = new SqlCommand("insertupdate", conn);
              cmd.CommandType = CommandType.StoredProcedure;
  
              if (userTextBox.Text == "")
              {
                  num = 0;
              }
              else
              {
                  num = int.Parse(userTextBox.Text);
              }
  
              cmd.Parameters.AddWithValue("@id", num);
              cmd.Parameters.AddWithValue("@name", nameTextBox.Text);
              cmd.Parameters.AddWithValue("@PhoneNumber", phoneNumberTextBox.Text);
  
              if (saveButton.Text == "Save")
              {
                  cmd.Parameters.AddWithValue("@Flag", 'S');
              }
              else
              {
                  cmd = new SqlCommand("UpdateUser", conn);
                  cmd.CommandType = CommandType.StoredProcedure;
              }
  
              try
              {
                  conn.Open();
  
                  if (cmd.ExecuteNonQuery() &gt; 0 &amp;&amp; saveButton.Text == "Save")
                  {
                      MessageBox.Show("record inserted");
                  }
                  else
                  {
                      MessageBox.Show("record Update");
                  }
              }
              catch (Exception ex)
              {
                  MessageBox.Show(ex.Message);
                  conn.Close();
              }
          }
  
          public void clearfilds()
          {
              userTextBox.Text = "";
              nameTextBox.Text = "";
              phoneNumberTextBox.Text = "";
              saveButton.Text = "Save";
          }
  
          public void ShowRecordAfterSaveAndEdit()
          {
              cmd = new SqlCommand("UserInformation", conn);
              data = new SqlDataAdapter(cmd);
              ds = new DataSet();
              data.Fill(ds);
              this.userdataGridView1.DataSource = ds.Tables[0];
              conn.Close();
          }
  
          private void ShowData(object sender, EventArgs e)
          {
              int show = userdataGridView1.CurrentRow.Index;
              userTextBox.Text = userdataGridView1.CurrentRow.Cells["Id"].Value.ToString();
              nameTextBox.Text = userdataGridView1.CurrentRow.Cells["Name"].Value.ToString();
              phoneNumberTextBox.Text = userdataGridView1.CurrentRow.Cells["PhoneNumber"].Value.ToString();
          }
      }
  }
  </code></pre>