我是jitendra kumar工作在庫存管理系統和 我們有代碼在我的dataGridview中添加一行在windows應用程序中使用列表提交,但是當我們通過點擊按鈕AddToCart購買新產品時,它將覆蓋dataGridview中的前一行,但不會添加另一個新行dataGridview。如何在C#窗口應用程序中動態添加行datagridview就像在Asp.net Web應用程序中添加到購物車而不使用數據庫
我想在dataGridview中添加新行,並且所有以前的行都保留在我的dataGridview中,就像Web應用程序項目中的添加到購物車選項一樣,請任何人都可以提出一些想法來解決此問題。下面
感謝
是我的代碼
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;
}
}
}
非常感謝你..它的工作對我罰款 – user2342574
現在我想用在cell_click EVNT或按鈕單擊事件 – user2342574
答案編輯成包括C#代碼刪除從DataGridView行刪除邏輯。 – user3185569