title: Deleting a row in grid view,in which data and column names are given from code behind
body: <p>I need to delete a row from an GridView,i Used an different approach ,all the things are done in code behind.I tried To delete a row when user clicks the Delete link button but the row row is not getting deleted, I tried In row deleting event but i am getting a stack overflow exception.</p>
  
  <p>Adding Column Names:</p>
  
  <pre><code>DataTable dtToGrid = new DataTable();
  DataColumn dc = dtToGrid.Columns.Add("S.No", typeof(int));
  dc.AutoIncrement = true;
  dc.AutoIncrementSeed = 1;
  dc.AutoIncrementStep = 1;
  dtToGrid.Columns.Add("ItemName", typeof(string));
  dtToGrid.Columns.Add("Quantity", typeof(string));
  dtToGrid.Columns.Add("Unit", typeof(string));
  dtToGrid.Columns.Add("RatePer Unit", typeof(string));
  dtToGrid.Columns.Add("Amount", typeof(string));
  Session["dtToGrid"] = dtToGrid;   
  </code></pre>
  
  <p>Adding Row Values: </p>
  
  <pre><code>double sum = 0;
  
  grd.Visible = true;
  
  DataTable dtToGrid = (DataTable)Session["dtToGrid"];
  
  DataRow drToGrid = dtToGrid.NewRow();
  
  drToGrid["ItemName"] =ddlProduct.SelectedItem.Value;
  drToGrid["Quantity"] = txtQty.Text;
  drToGrid["Unit"]=Unit.Value;
  drToGrid["RatePer Unit"]=costperunit.Value;
  drToGrid["Amount"]=txtAmount.Text;                                  
  
  dtToGrid.Rows.Add(drToGrid);
  
  grd.DataSource = dtToGrid;
  grd.DataBind();
  for (int x = 0; x &lt; grd.Rows.Count; x++)
  {
      sum += Convert.ToDouble(grd.Rows[x].Cells[5].Text);
  }
  if (totalAmountINcludingTax.Text == " ")
  {
      sum = (sum * Convert.ToDouble(Tax.Value)) / 100 + sum;
      totalAmountINcludingTax.Text = sum.ToString();
  }
  else
  {
      sum = (sum * Convert.ToDouble(Tax.Value)) / 100 +Convert.ToDouble( totalAmountINcludingTax.Text);
      totalAmountINcludingTax.Text = sum.ToString();
  }
  </code></pre>
  
  <p>Row Deleting:</p>
  
  <pre><code>protected void grd_RowCommand(object sender, GridViewCommandEventArgs e)
  {
      if (e.CommandName == "Delete")
      {
          int Prod_Id = Convert.ToInt32(e.CommandArgument);
          grd.DeleteRow(Prod_Id);
      }
  }
  </code></pre>
  
  <p>Aspx:</p>
  
  <pre><code>&lt;asp:GridView ID="grd" runat="server" AutoGenerateColumns="true" 
      onrowdeleting="grd_RowDeleting" onrowcommand="grd_RowCommand"&gt;
  
      &lt;Columns&gt;
          &lt;asp:CommandField ShowDeleteButton="True" /&gt;
  
          &lt;asp:TemplateField&gt;
              &lt;ItemTemplate&gt;
                  &lt;asp:LinkButton ID="lbtnDelete"  runat="server" CommandName="delete" title="Delete"
                      OnClientClick="return confirm('Do you Want to Delete this Record?');"&gt;
                  &lt;/asp:LinkButton&gt;
              &lt;/ItemTemplate&gt;
          &lt;/asp:TemplateField&gt;
      &lt;/Columns&gt;
  &lt;/asp:GridView&gt;
  </code></pre>