user2342574
user2342574

Reputation: 107

How to add rows in datagridview dynamically in C# windows application just like Add to Cart in Asp.net Web application without using database

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.

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.

Thanks

below is my 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<field> 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<field>();
        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;
     }
  }
}

enter image description here

Upvotes: 0

Views: 5232

Answers (1)

Zein Makki
Zein Makki

Reputation: 30042

Problem With you Code:

You're creating a new list each time you click Save with:

data = new List<field>();

This Line needs to be removed.

Replace List<field> data = null; With List<field> data = new List<field>();

Solution 1:

Using BindingList<T> instead of List<T>, like:

BindingList<field> data = new BindingList<field>();

Instead Of: List data = null;

Using this approach, when you call data.Add(f); the row will be automatically added to the grid. No need to reassign the DataSource or refresh it.

Solution 2:

Replace:

dataGridView1.DataSource = data.ToList(); //.ToList() is useless.

With:

dataGridView1.DataSource = null;
dataGridView1.DataSource = data;

Edited After Comments:

To Delete a Row or Multiple selected Rows, the button click logic:

foreach(DataGridViewRow gridRow in dataGridView1.SelectedRows)
{
    var rowObject = gridRow.DataBoundItem as field; // field is the class name

    data.Remove(rowObject);
}

Upvotes: 3

Related Questions