0

我有一个 datagridview,我想将一个 linq 查询绑定到它。

这是在我的班级水平:

   private readonly SuburbanPortalEntities _entities;
   private List<PaymentType> _paymentTypes;

这是我负载中的代码:

  _paymentTypes = (from pt in _entities.PaymentTypes
            where pt.CorporationId.Equals(_currentcorp.CorporationId)
            select pt).ToList();
  dataGridView_PaymentTypes.DataSource = _paymentTypes;

这是我的插入,在这一点上,它什么都不做:

private void button_Insert_Click(object sender, EventArgs e)
{
  var row = new PaymentType();
  row.IsActive = false;
  row.IsAdded = true;
  row.CorporationId = _currentcorp.CorporationId;
  row.TokenId = _token.TokenId;
  row.PaymentTypeId = Guid.NewGuid();
  row.ExcludeCreditCodes = 9;
  _paymentTypes.Add(row);
}

还有我的简单保存按钮:

_entities.SaveChanges();

它正在处理现有记录。当我对它们进行更改时,它会更改数据库中的数据。它不适用于新记录,我没有删除,因为我不知道该怎么做。

我宁愿允许 datagridview 在行上添加记录而不是按钮。此外,通过删除,允许 datagridview 处理删除。

有什么建议么?

4

1 回答 1

0

要将新数据插入 sql server,您可以尝试以下操作:

private void button_Insert_Click(object sender, EventArgs e)
{
  var row = new PaymentType();
  row.IsActive = false;
  row.IsAdded = true;
  row.CorporationId = _currentcorp.CorporationId;
  row.TokenId = _token.TokenId;
  row.PaymentTypeId = Guid.NewGuid();
  row.ExcludeCreditCodes = 9;
  _entities.PaymentTypes.Add(row);  //for linq to sql, you should use InsertOnSubmit()
  _entities.SaveChanges();
}
于 2013-06-29T15:48:29.337 回答