如何将List中的更改反映回数据库? [英] How reflecting changes in the List back on the database?

查看:104
本文介绍了如何将List中的更改反映回数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在UI上有一个DataGridView(一个胜利形式)和一个存储在List< earning>中的模型对象。网格中显示。



由于这是MVP模式,因此从服务返回的DataSet将被管理器类转换为模型列表(Earning),然后它由演示者作为BindingSource传递给视图。



列表中的所有对象都正确显示在网格上。现在我要做的是当用户在网格中进行更改时,这些更改应该反映在列表中(在对象属性中),然后应该在数据库中更新。



在这种情况下,我使用数据绑定的方式是否正确?

因为我已经将网格绑定到列出如何将列表上的更改保存回数据库?

是否可以使用反射来更新视图中的列表?





查看



I have a DataGridView on the UI (a win form) and model objects stored in a List <earning> are shown in the Grid.

Since this is MVP pattern, the DataSet returned from the service is converted to a list of Models (Earning) by the manager class and then the it's passed to the view by the presenter as a BindingSource.

All objects in the List are properly shown on the grid. Now What I'm trying to do is when user do the changes in the grid those changes should be reflected in the list (in object properties) and then should be updated in the database.

Is it correct the way I've used data binding in this case?
Since I've bound the grid to list how I save the changes on the list back on the database?
Is it possible to use reflection to update the list from the view?


View

public System.Collections.Generic.IList<Earning> EarningDetails
{
   set {dgEmployeeEarnings.DataSource= value;}
}





Presenter Class



Presenter Class

private void ShowEarnings()
{
   _WageView.EarningDetails= _WageManager.PrepareEarnings ();
}





WageManager课程





WageManager Class

public List<Earning> PrepareEarnings()
{
   return GetEarningObjedtList(CalculateEarnings(WagePeriodStartDate, WagePeriodEndDate));
}
    
    
private DataTable CalculateEarnings(DateTime startDate, DateTime endDate)
{
   return _DataService.GetEarnings(startDate, endDate);
}
    
private List<Earning > GetEarningObjedtList(DataTable dt)
{
   var convertedList = (from rw in dt.AsEnumerable()
      select new Earning()
      {
         EmployeeID=Convert.ToInt32(rw[0]),
         WorkDays= Convert.ToInt32 (rw[2]),
         DayOffs = Convert.ToInt32(rw[3]),
         LeaveDays  = Convert.ToInt32(rw[4]),
         ExtraShifts = Convert.ToInt32(rw[5]),
         ExtraShiftAmount = Convert.ToDecimal (rw[7]),
         BasicSalaryAmount = Convert.ToDecimal(rw[8]),
         BudjetoryAllowance = Convert.ToDecimal(rw[9]),
         NoPayDays = Convert.ToInt32(rw[10]),
         LessNoPayAmount = Convert.ToDecimal(rw[11]),
         AmountForEPF = Convert.ToDecimal(rw[12]),
         OverTimeAmount = Convert.ToDecimal(rw[13]),
         BroughtForwardAmount = rw[14] == DBNull.Value ? 0 : Convert.ToDecimal(rw[14]),
      }).ToList();
    
   return convertedList;
}

推荐答案

没有足够的信息来了解您的数据是如何相关的。

来自使用WinForms和DataSet进行类似的项目:



1.确定哪些数据已更改:

There's not enough information to know how your data is related.
From doing similar projects with WinForms and DataSets:

1. Determine which data was changed:
private void theDataGridView_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
int c = e.ColumnIndex;
int r = e.RowIndex;
//insert or update DataTable
}





2.更新数据库并保存,然后重新填写表格



2. Update database and Save, then re-fill form

this.Validate();
this.theBindingSource.EndEdit();
TableAdapter.Insert();
//or TableAdapter.Update();
RefreshFormData();





如果您希望在每次更改单元格后更新列表并在结尾保存所有数据,则需要写入DataTable并重新填充您的列表和更新数据库在另一个事件中,例如提交按钮。



If you wanted the list to update after each cell change and save all data at the end, would need to write to the DataTable and repopulate your list and update database in another event e.g. a Commit button.


这篇关于如何将List中的更改反映回数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆