2
\$\begingroup\$

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

View

class Form
{
    public BindingSource EarningDetails
    {
        set {dgEmployeeEarnings.DataSource= value;}
    }
}

Presenter Class

class WagePresenter
{    
    BindingSource BS = new BindingSource();

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

WageManager Class

class WageManager
{    
    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;
            }
}

Since this is MVP pattern, the DataSet returned from the service is converted to a list of Models (Earning) and then 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 it should be updated in the database.

  1. Is it possible to do this with Lists/ ILists?
  2. Any other notable flows in this approach?
\$\endgroup\$
2
\$\begingroup\$

I believe you will want to use a BindingList, which can notify when entries are updated.

You will need to handle the events raised in order to reflect the changes back to the original source.

See these sources for more:

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.