Click here to Skip to main content
15,744,817 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
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?


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

Presenter Class
private void ShowEarnings()
   _WageView.EarningDetails= _WageManager.PrepareEarnings ();

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()
         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]),
   return convertedList;
Updated 19-May-14 7:13am

1 solution

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. Update database and Save, then re-fill form
//or TableAdapter.Update();

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.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900