title: How to add rows in datagridview dynamically in C# windows application just like Add to Cart in Asp.net Web application without using database
body: <p>I am jitendra kumar working on a Inventory Management System and
  we have code to add a row in my dataGridview in windows application using list filed but when we buy new product by clicking on a button AddToCart then it overrides the previous row in dataGridview but not add another new row dataGridview.</p>
  
  <p>I want to add new row in dataGridview and also all retains all previous rows in my dataGridview just like Add to Cart option in Web Application project,
  please anyone can give some idea to solve this problem.</p>
  
  <p>Thanks</p>
  
  <p>below 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.Windows.Forms;
  using System.Data.SqlClient;
  using CrystalDecisions.CrystalReports.Engine;
  using CrystalDecisions.Shared;
  
  namespace SIMS1
  {
  
  public partial class Sales : Form
  {
      DataTable dt;
      List&lt;field&gt; data = null; decimal Total = 0;
      int Invoiceid,qty;
      decimal unitprize,Total_amount;
      public Sales()
      {
          InitializeComponent();
          binddgvstock();
  
  }
  
      private void btn_save_Click(object sender, EventArgs e)
      {
  
          data = new List&lt;field&gt;();
          string invoiceno = txt_invoiceno.Text;
          if (string.IsNullOrEmpty(invoiceno))
          {
  
              string productname = txt_productname.Text;
              decimal unitprize = Convert.ToDecimal(txt_unitprize.Text);
              int Quantity = Convert.ToInt32(txt_qty.Text);
              decimal total = Convert.ToDecimal(txt_totalamount.Text);
              field f = new field();
              f.ProductName = productname;
              f.UnitPrize = unitprize;
              f.Quantity = Quantity;
              f.TotalAmount = total;
              data.Add(f);
  
  
              dataGridView1.AutoGenerateColumns = false;
              dataGridView1.DataSource = data.ToList();
  
          }
          else
          {
              string productname = txt_productname.Text;
              decimal unitprize = Convert.ToDecimal(txt_unitprize.Text);
              int Quantity = Convert.ToInt32(txt_qty.Text);
              decimal total = Convert.ToDecimal(txt_totalamount.Text);
              field f = new field();
              f.ProductName = productname;
              f.UnitPrize = unitprize;
              f.Quantity = Quantity;
              f.TotalAmount = total;
              data.Add(f);
  
              dataGridView1.AutoGenerateColumns = false;
              dataGridView1.DataSource = data.ToList();
  
          }
  
          txt_productname.Text = "";
          txt_unitprize.Text = "";
          txt_qty.Text = "";
          txt_totalamount.Text = "";
  
          }
       }
   [Serializable]
   public class field
   {
  
      public int InvoiceId
      {
          get;
          set;
      }
      public string InvoiceNo
      {
          get;
          set;
      }
      public DateTime InvoiceDate
      {
          get;
          set;
      }
      public string CustomerId
      {
          get;
          set;
      }
      public string CustomerName
      {
          get;
          set;
      }
      public string ProductName
      {
          get;
          set;
      }
      public decimal UnitPrize
      {
          get;
          set;
      }
      public int Quantity
      {
          get;
          set;
      }
      public decimal DISC
      {
          get;
          set;
      }
      public decimal TotalAmount
      {
          get;
          set;
      }
      public decimal Total
      {
          get;
          set;
       }
    }
  }
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/doPEU.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/doPEU.png" alt="enter image description here"></a></p>