DataGridView不与数据库更新,只在函数内部与参数(object,EventArgs)工作
本文关键字:object 参数 EventArgs 工作 内部 数据库 更新 DataGridView 函数 | 更新日期: 2023-09-27 18:05:51
我有一个数据表与一些数据,我绑定它与我的DataGridView与此代码。数据库正在更新,但gridview没有更新:
private void showdetails()
{
dataGridViewInventory.DataSource = null;
dataGridViewInventory.Columns.Clear();
var st= (from a in db.productInvertoryTables
select a).ToList();
dataGridViewInventory.DataSource = st;
dataGridViewInventory.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
dataGridViewInventory.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.None;
var st1 = from k1 in db.cartTables
select k1;
dataGridView2cart.DataSource = st1;
dataGridView2cart.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
dataGridView2cart.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.None;
dataGridView2cart.Refresh();
}
添加到购物车-
public void AddToCart(int id,int count)
{
productInvertoryTable t = db.productInvertoryTables.SingleOrDefault(x => x.Id == id);
cartTable ct = new cartTable();
if (db.cartTables.SingleOrDefault(q => q.prod_Id == t.Id) == null)
{
ct.datee = DateTime.Today.ToString();
ct.prod_Id = t.Id;
ct.name = t.name;
ct.quantity = count;
ct.pricee = t.sellprice * count;
db.cartTables.InsertOnSubmit(ct);
t.quantity -= count;
db.SubmitChanges();
}
else
{
cartTable cct = db.cartTables.SingleOrDefault(q => q.prod_Id == t.Id);
cct.quantity += count;
t.quantity -= count;
cct.pricee += (count * t.sellprice);
cct.datee = DateTime.Today.ToString();
db.SubmitChanges();
showdetails();
}
}
之前after- HP笔记本电脑商品加入购物车后应减少1
更新后的代码:
添加到购物车:
public void AddToCart(int id,int count)
{
productInvertoryTable t = db.productInvertoryTables.SingleOrDefault(x => x.Id == id);
cartTable ct = new cartTable();
if (db.cartTables.SingleOrDefault(q => q.prod_Id == t.Id) == null)
{
ct.datee = DateTime.Today.ToString();
ct.prod_Id = t.Id;
ct.name = t.name;
ct.quantity = count;
ct.pricee = t.sellprice * count;
db.cartTables.InsertOnSubmit(ct);
t.quantity -= count;
db.SubmitChanges();
}
else
{
cartTable cct = db.cartTables.SingleOrDefault(q => q.prod_Id == t.Id);
cct.quantity += count;
t.quantity -= count;
cct.pricee += (count * t.sellprice);
cct.datee = DateTime.Today.ToString();
db.SubmitChanges();
}
showdetails(id); //Move outside to refresh the main and cart grid after save/update
}
更新网格:
private void showdetails(int id)
{
//dataGridViewInventory.DataSource = null;
//dataGridViewInventory.Columns.Clear();
var st= from a in db.productInvertoryTables
select a
dataGridViewInventory.DataSource = st.ToList(); //Updated
dataGridViewInventory.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
dataGridViewInventory.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.None;
var st1 = from k1 in db.cartTables
where k1.prod_Id == id
select k1;
dataGridView2cart.DataSource = st1.ToList(); //Updated
dataGridView2cart.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
dataGridView2cart.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.None;
}
让我知道它是否有效,最诚挚的问候。
您可以尝试使用Binding List代替List
吗?它支持数据绑定,并在集合被修改时触发事件。
var bindingList = new BindingList<ProductInvertoryTable>(st);
然后将此变量赋值给数据源。
var st= from a in db.productInvertoryTables
select a;
var bindinglist = new BindingList<ProductInvertoryTable>(st.ToList());
dataGridViewInventory.DataSource = bindinglist;
dataGridViewInventory.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
dataGridViewInventory.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.None;