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 now
I want to update row in 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;
}
}
}
it's quite easy actually,first off you need to move these lines out below the if-else block since it's a needless repeitition
dataGridView1.AutoGenerateColumns = false;
dataGridView1.DataSource = data.ToList();
Then replace the final line to:
var dt=dataGridView1.DataSource as DataTable;
dt.Rows.Add(data.ToArray());
Assumming that your datasource is not null that is
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.