title: Update insert and delete gridview without database
body: <p>I am able to delete and insert but I don't have idea how to select and update.  I am using <code>DataTable</code> and I don't have to to use database to update it</p>
  
  <p>Markup:</p>
  
  <pre><code>&lt;div&gt;
      Name &lt;asp:TextBox ID="TxtName" runat="server" &gt;&lt;/asp:TextBox&gt;
      Age &lt;asp:TextBox ID="txtAge" runat="server" &gt;&lt;/asp:TextBox&gt;
  
      &lt;asp:Button ID="btnInsert" Text="Insert"  runat="server" OnClick="btnInsert_Click"  /&gt;
  
      &lt;asp:GridView ID="grdView" runat="server" OnRowCommand="grdView_RowCommand" OnRowDeleting="grdView_RowDeleting" OnSelectedIndexChanged="grdView_SelectedIndexChanged"   &gt;
  
      &lt;Columns&gt;
          &lt;asp:TemplateField&gt;               
                  &lt;ItemTemplate&gt;
                      &lt;asp:Button ID="btn" runat="server" Text="Delete"  CommandName="delete" /&gt;
                  &lt;/ItemTemplate&gt;
                  &lt;EditItemTemplate&gt;
  
                  &lt;/EditItemTemplate&gt;
                  &lt;%--&lt;ItemTemplate&gt;
                      &lt;asp:Button ID="btnEdit" runat="server" Text="Edit" CommandName="Edit" /&gt;
                  &lt;/ItemTemplate&gt;--%&gt;
              &lt;/asp:TemplateField&gt;
              &lt;asp:TemplateField&gt;
                  &lt;ItemTemplate&gt;
                      &lt;asp:Button runat="server" CommandName="Change" ID="btnEdit" Text="Edit" /&gt;
                  &lt;/ItemTemplate&gt;
              &lt;/asp:TemplateField&gt;
          &lt;/Columns&gt;
      &lt;/asp:GridView&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>Code:</p>
  
  <pre><code>public partial class WebForm1 : System.Web.UI.Page
  {
      protected void Page_Load(object sender, EventArgs e)
      {
  
      }
      public static DataTable dt;
      public static int i = 1;
      protected void btnInsert_Click(object sender, EventArgs e)
      {
          Session["datatable"] = dt;
          dt = new DataTable();
          DataRow dr = null;
          dt.Columns.Add(new DataColumn("RowNumber", typeof(int)));
          dt.Columns.Add(new DataColumn("Column1", typeof(string)));
          dt.Columns.Add(new DataColumn("Column2", typeof(string)));
          //
          if (Session["datatable"] != null) 
          {
  
              dt = (DataTable)Session["datatable"];
              dr = dt.NewRow();
              dr["RowNumber"] = i;
              dr["Column1"] = TxtName.Text;
              dr["Column2"] = txtAge.Text;
              dt.Rows.Add(dr);
          }
          else
          {
               dr = dt.NewRow();
              dr["RowNumber"] = i;
              dr["Column1"] = TxtName.Text;
              dr["Column2"] = txtAge.Text;
              dt.Rows.Add(dr);
          }
  
          grdView.DataSource = dt;
          grdView.DataBind();
          i++;
      }
  
      //protected void btn_Click(object sender, EventArgs e)
      //{
      //    dt = (DataTable)Session["datatable"];
      //    if (dt.Rows.Count &gt; 0)
      //    {
  
      //        dt.Rows.RemoveAt(Convert .ToInt16 ( grdView.SelectedRow) );
      //        grdView .DataSource = dt;
      //        grdView.DataBind();
  
      //    }
      //}
  
      protected void grdView_RowCommand(object sender, GridViewCommandEventArgs e)
      {
          if (e.CommandName == "delete")
          {
              dt = (DataTable)Session["datatable"];
              if (dt.Rows.Count &gt;= 0)
              {
  
                  dt.Rows.RemoveAt(Convert.ToInt16(grdView.SelectedRow));
                  grdView.DataSource = dt;
                  grdView.DataBind();
  
              }
              else if (e.CommandName == "Change")
              {
                  TxtName.Text = grdView .Rows[grdView .SelectedIndex].Cells[1].Text;
              }
          }
      }
  
      protected void grdView_RowDeleting(object sender, GridViewDeleteEventArgs e)
      {
          grdView.DataSource = dt;
          grdView.DataBind();
      }
  
      protected void grdView_SelectedIndexChanged(object sender, EventArgs e)
      {
  
      }
  }
  </code></pre>