Advertisement
davidb37

list being overwirtten when cmd new is pressed

Aug 16th, 2013
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 16.55 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Text.RegularExpressions;
  9. using System.Windows.Forms;
  10. using Telerik.WinControls.UI;
  11. using HallmarkSolutions.PAMS.Interop.EntityFramework;
  12. using HallmarkSolutions.PAMS.Interop.DataObjects;
  13. using HallmarkSolutions.PAMS.Interop.DataObjects.Employee;
  14.  
  15. using UtilityLibrary;
  16. using Telerik.WinControls;
  17.  
  18. using TelerikReports;
  19. using System.Collections.ObjectModel;
  20. namespace HallmarkSolutions.PAMS.Interop.Forms
  21. {
  22.     public partial class EmployeeAppraisalForm : Telerik.WinControls.UI.RadForm
  23.     {
  24.  
  25.         #region Fields
  26.         private string employeeName;
  27.         private int userid;
  28.         private PamsContext pamsContext = new PamsContext();
  29.  
  30.         //  private List<AppraisalList> apprList;
  31.         BindingList<AppraisalList> apprList;
  32.                
  33.         int recordid;
  34.         int empNo;
  35.         DateTime apprisal_date;
  36.         bool dataChanged;
  37.         private bool viewRights;
  38.  
  39.  
  40.        
  41.         #endregion
  42.  
  43.         public EmployeeAppraisalForm(int id, string username, bool viewRights, bool addRights, bool editRights, bool deleteRights)
  44.         {
  45.             InitializeComponent();
  46.             empNo = id;
  47.             employeeName = username;
  48.             this.Text += " "+employeeName.ToString();
  49.             this.viewRights = viewRights;
  50.            
  51.  
  52.  
  53.             if (!addRights)
  54.                 cmdnew.Enabled = false;
  55.  
  56.             if (!deleteRights)
  57.                 cmddelete.Enabled = false;
  58.  
  59.             if (!editRights)
  60.             {
  61.                 rdaprisal.ReadOnly = true;
  62.                 rdnotes.ReadOnly = true;
  63.                 cbotalent.Enabled = false;
  64.                 grvappraisal.ReadOnly = true;
  65.                 btnsave.Enabled = true;
  66.                
  67.             }
  68.  
  69.                
  70.         }
  71.        
  72.  
  73.         private void button2_Click(object sender, EventArgs e)
  74.         {
  75.            
  76.              
  77.         }
  78.  
  79.  
  80.         private void DataValueChanged(object sender, EventArgs e)
  81.         {
  82.             try
  83.             {
  84.                 dataChanged = true;
  85.                 if (!btnsave.Enabled)
  86.                     btnsave.Enabled = true;
  87.                  
  88.             }
  89.             catch (Exception ex)
  90.             {
  91.                 ExceptionBox.Show("An error occurred in the DataValueChanged event.", ex);
  92.             }
  93.         }
  94.  
  95.         private void EmployeeAppraisalForm_Load(object sender, EventArgs e)
  96.         {
  97.          //   try
  98.         //    {
  99.  
  100.                 rdtalentrating.DataSource = pamsContext.GetTalentRating();
  101.                 rdtalentrating.DisplayMember = "value";
  102.                 rdtalentrating.ValueMember = "key";
  103.  
  104.  
  105.  
  106.  
  107.                 rdreadiness.DataSource = pamsContext.GetAppraisalReadiness();
  108.                 rdreadiness.DisplayMember = "value";
  109.                 rdreadiness.ValueMember = "key";
  110.  
  111.                 BindAppraisalList(false);
  112.  
  113.                 rdnotes.TextChanged += DataValueChanged;
  114.                 rdnextappraisaldate.TextChanged += DataValueChanged;
  115.                 rdaprisal.ValueChanged += DataValueChanged;
  116.                 rdreadiness.TextChanged += DataValueChanged;
  117.                 performancescore.TextChanged += DataValueChanged;
  118.                 rdtalentrating.TextChanged += DataValueChanged;
  119.                 this.rdnextappraisaldate.DateTimePickerElement.NullDate = new DateTime(1900, 01, 01);
  120.                 this.rdnextappraisaldate.NullText = "";
  121.  
  122.       //      }
  123.        //     catch (Exception ex)
  124.        ///     {
  125.              //   ExceptionBox.Show("An error occurred in the Form load event.", ex);
  126.        //     }
  127.      
  128.  }
  129.  
  130.  
  131.    
  132.         private void BindAppraisalList(bool postUpdate)
  133.         {
  134.  
  135.             try
  136.             {
  137.                     BindingSource bs = new BindingSource();
  138.                     bs.DataSource =  pamsContext.GetAppraisals(empNo);
  139.                     grvappraisal.DataSource = bs;
  140.                
  141.                
  142.             }
  143.             catch (Exception ex)
  144.             {
  145.                 ExceptionBox.Show("An error occurred in the Bind Appraisal event.", ex);
  146.             }
  147.         }
  148.  
  149.  
  150.    
  151.  
  152.         private void cmdnew_Click(object sender, EventArgs e)
  153.         {
  154.                 int contractId = 0;
  155.            
  156.                 List<ContractSelectionList> contracts = pamsContext.GetEmployeeContracts(empNo);
  157.  
  158.                 if (contracts.Count == 0)
  159.                 {
  160.                     MessageBox.Show(string.Format("{0} ({1}) does not have any contracts!", "", empNo), "Create Appriasal Records", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
  161.  
  162.                 }
  163.                 else
  164.                 {
  165.                     if (contracts.Count > 1)
  166.                     {
  167.                         EmployeeContractSelectionForm form = new EmployeeContractSelectionForm("", contracts);
  168.                         form.ShowDialog(this);
  169.                         contractId = form.ContractId;
  170.                     }
  171.                     else
  172.                     {  contractId = contracts[0].ContractId;
  173.                     }
  174.  
  175.              
  176.                              
  177.                      EmployeeTransactionDateForm form2 = new EmployeeTransactionDateForm();
  178.                      form2.ShowDialog(this);
  179.                     contractId = contractId;
  180.                     DateTime? dateSample;
  181.                     dateSample = null;
  182.  
  183.                    
  184.                        apprList.Add(new AppraisalList(empNo, contractId, contractId, Convert.ToDateTime("15/08/2013"), "david", Convert.ToDateTime("15/08/2013"), 90, "test", Convert.ToDateTime("15/08/2013"), 1, 2, 3));
  185.  
  186.  
  187.                     rebindgrid(apprList);
  188.  
  189.                 }
  190. //        BindAppraisalList(false);
  191.        
  192.          
  193.                
  194.          
  195.         }
  196.  
  197.         private void rebindgrid(BindingList<AppraisalList> bd)
  198.         {
  199.  
  200.             BindingSource bs = new BindingSource();
  201.            
  202.             bs.DataSource = bd;
  203.             grvappraisal.DataSource = bs;
  204.         }
  205.      
  206.  
  207.         private void btnsave_click(object sender, EventArgs e)
  208.         {
  209.  
  210.  
  211.             try
  212.             {
  213.                 if (dataChanged)
  214.                 {
  215.                     if (  pamsContext.SaveChanges())
  216.                         Close();
  217.                 }
  218.                 else
  219.                     Close();
  220.             }
  221.             catch (Exception ex)
  222.             {
  223.                 ExceptionBox.Show("An error occurred in the Save button Click event.", ex);
  224.             }
  225.          
  226.              
  227.         }
  228.  
  229.          
  230.  
  231.  
  232.         private void grvappraisal_Click(object sender, EventArgs e)
  233.         {
  234.               recordid = Convert.ToInt32(grvappraisal.SelectedRows[0].Cells[0].Value);//grvappraisal.SelectedRows.
  235.              
  236.     }
  237.  
  238.         private void grvappraisal_CurrentRowChanged(object sender, CurrentRowChangedEventArgs e)
  239.        
  240.         {
  241.  
  242.             try
  243.             {
  244.  
  245.                 if (grvappraisal.SelectedRows.Count > 0)
  246.                 {
  247.                    
  248.                  
  249.                         //appraisal appraisal = grvappraisal.SelectedRows[0].DataBoundItem as appraisal;
  250.                         AppraisalList appraisal = grvappraisal.SelectedRows[0].DataBoundItem as AppraisalList;
  251.                         appraisal.Next_Date = rdnextappraisaldate.Value;
  252.                         //appraisal.Readiness = Convert.ToInt32(rdreadiness.SelectedValue);
  253.                         //appraisal.rating = Convert.ToInt32(rdtalentrating.SelectedValue);
  254.                         //appraisal.submitted = rdaprisal.Value;
  255.                         //  appraisal.notes = rdnotes.Text;
  256.                    
  257.                 }
  258.  
  259.             }
  260.             catch (Exception ex)
  261.             {
  262.                 ExceptionBox.Show("An error occurred in the CurrentRowChanged event.", ex);
  263.             }
  264.         }
  265.        
  266.         private void grvappraisal_RowsChanged(object sender, GridViewCollectionChangedEventArgs e)
  267.         {
  268.              
  269.          
  270.         }
  271.  
  272.  
  273.         private void grvappraisal_SelectionChanged(object sender, EventArgs e)
  274.         {
  275.             try
  276.             {
  277.  
  278.                 if (grvappraisal.SelectedRows.Count > 0)
  279.                 {
  280.  
  281.                     AppraisalList appraisal = grvappraisal.SelectedRows[0].DataBoundItem as AppraisalList;
  282.                   rdnextappraisaldate.Value=  appraisal.Next_Date ;
  283.                  
  284.                     //appraisal appraisal = grvappraisal.SelectedRows[0].DataBoundItem as appraisal;
  285.                     //rdnextappraisaldate.Value = appraisal.next_date;
  286.                     //rdreadiness.SelectedValue = appraisal.Readiness;
  287.                     //rdtalentrating.SelectedValue = appraisal.rating;
  288.                     //appraisal.submitted = rdaprisal.Value;
  289.                     //rdnotes.Text = appraisal.notes;
  290.  
  291.  
  292.                 }
  293.  
  294.             }
  295.             catch (Exception ex)
  296.             {
  297.                 ExceptionBox.Show("An error occurred in the grvappraisal_SelectionChanged event.", ex);
  298.             }
  299.         }
  300.  
  301.         private void cmdcancel_Click(object sender, EventArgs e)
  302.         {
  303.             try
  304.             {
  305.                 if (dataChanged)
  306.                 {
  307.                     DialogResult result = CheckForChanges();
  308.  
  309.                     if (result == DialogResult.Cancel)
  310.                         return;
  311.                     else
  312.                         Close();
  313.                 }
  314.                 else
  315.                     Close();
  316.             }
  317.             catch (Exception ex)
  318.             {
  319.                 ExceptionBox.Show("An error occurred in the Close button click event.", ex);
  320.             }
  321.  
  322.         }
  323.  
  324.  
  325.         private DialogResult CheckForChanges()
  326.         {
  327.             DialogResult result = DialogResult.Cancel;
  328.  
  329.             try
  330.             {
  331.  
  332.              
  333.                     result = MessageBox.Show("The appraisal form record details have changed. Do you want to keep the changes?", "Keep Changes", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button3);
  334.  
  335.                     switch (result)
  336.                     {
  337.                         case DialogResult.Yes:
  338.                             if (!pamsContext.SaveChanges())
  339.                                 return DialogResult.Cancel;
  340.                             break;
  341.                         case DialogResult.No:
  342.                             dataChanged = false;
  343.                             break;
  344.                    
  345.                 }
  346.             }
  347.             catch (Exception ex)
  348.             {
  349.                 ExceptionBox.Show("An error occurred in the CheckForChanges method.", ex);
  350.             }
  351.  
  352.             return result;
  353.         }
  354.         private void rdnotes_TextChanged(object sender, EventArgs e)
  355.         {
  356.             dataChanged = true;
  357.         }
  358.  
  359.         private void grvappraisal_RowsChanging(object sender, GridViewCollectionChangingEventArgs e)
  360.         {
  361.          
  362.                  
  363.         }
  364.  
  365.         private void grvappraisal_CellDoubleClick(object sender, GridViewCellEventArgs e)
  366.         {
  367.            
  368.         }
  369.  
  370.         private void rdaprisal_ValueChanged(object sender, EventArgs e)
  371.         {
  372.             dataChanged = true;
  373.         }
  374.  
  375.         private void rdtalentrating_VisibleChanged(object sender, EventArgs e)
  376.         {
  377.             dataChanged = true;
  378.         }
  379.  
  380.         private void rdreadiness_SelectedValueChanged(object sender, EventArgs e)
  381.         {
  382.             dataChanged = true;
  383.         }
  384.  
  385.         private void rdnextappraisaldate_ValueChanged(object sender, EventArgs e)
  386.         {
  387.             dataChanged = true;
  388.         }
  389.  
  390.         private void performancescore_TextChanged(object sender, EventArgs e)
  391.         {
  392.             dataChanged = true;
  393.         }
  394.  
  395.         private void btnsave_Click_1(object sender, EventArgs e)
  396.         {
  397.            
  398.         }
  399.  
  400.         private void btnsave_Click_2(object sender, EventArgs e)
  401.         {
  402.             try
  403.             {
  404.                 if (dataChanged)
  405.                 {
  406.                     DialogResult result = CheckForChanges();
  407.  
  408.                     if (result == DialogResult.Cancel)
  409.                         return;
  410.                     else
  411.                     {
  412.                     }
  413.                       //  Close();
  414.                 }
  415.                 else
  416.                     Close();
  417.             }
  418.             catch (Exception ex)
  419.             {
  420.                 ExceptionBox.Show("An error occurred in the Close button click event.", ex);
  421.             }
  422.         }
  423.  
  424.         private void cmdprint_Click(object sender, EventArgs e)
  425.         {
  426.             try
  427.             {
  428.                 EmployeeTransactionDateForm form2 = new EmployeeTransactionDateForm();
  429.                 form2.ShowDialog(this);
  430.                 form2.Text = "Date Selection";
  431.  
  432.  
  433.                 AppraisalReportGeneratingForm form = new AppraisalReportGeneratingForm();
  434.                 form.CancelGeneration += OnCancelGeneration;
  435.                 form.Show(this);
  436.  
  437.                 backgroundWorker.RunWorkerAsync();
  438.  
  439.             }
  440.             catch (Exception ex)
  441.             {
  442.                 ExceptionBox.Show("An error occurred in the btnPrint_Click event.", ex);
  443.             }
  444.         }
  445.  
  446.         private void OnCancelGeneration()
  447.         {
  448.             try
  449.             {
  450.                 backgroundWorker.CancelAsync();
  451.             }
  452.             catch (Exception ex)
  453.             {
  454.                 ExceptionBox.Show("An error occurred in the OnCancelGeneration event.", ex);
  455.             }
  456.         }
  457.  
  458.      
  459.         private void backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
  460.         {
  461.             try
  462.             {
  463.                 Form[] forms = this.OwnedForms;
  464.  
  465.                 foreach (Form f in forms)
  466.                 {
  467.                     if (f is AppraisalReportGeneratingForm)
  468.                         f.Close();
  469.                 }
  470.  
  471.                 ReportViewer viewer = new ReportViewer();
  472.              
  473.              
  474.                 Telerik.Reporting.Report rpt = null;
  475.  
  476.                 AppraisalListReport reportfilter;
  477.                
  478.                 rpt = new AppraisalReport();
  479.                 ((AppraisalReport)rpt).DataSource = pamsContext.GetAppraisalReport(empNo,DateTime.MinValue,DateTime.MinValue);
  480.              
  481.              ((AppraisalReport)rpt).ReportTitle = "Appraisal  Report";
  482.                 viewer.ReportTitle = "Appraisal Report";
  483.                 viewer.Report = rpt;
  484.                 viewer.Refresh();
  485.                 viewer.ShowDialog(this);
  486.             }
  487.             catch (Exception ex)
  488.             {
  489.                 ExceptionBox.Show("An error occurred in the backgroundWorker_RunWorkerCompleted event.", ex);
  490.             }
  491.         }
  492.  
  493.         private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
  494.         {
  495.             try
  496.             {
  497.                 e.Result = pamsContext.GetAppraisalReport(empNo, DateTime.MinValue, DateTime.MinValue);
  498.             }
  499.             catch (System.IO.IOException ioex)
  500.             {
  501.                 backgroundWorker.ReportProgress(0, "I/O Error.");
  502.                 e.Result = ioex;
  503.             }
  504.             catch (Exception ex)
  505.             {
  506.                 backgroundWorker.ReportProgress(0, "An exception occurred.");
  507.                 e.Result = ex;
  508.             }
  509.         }
  510.  
  511.         private void cmddelete_Click(object sender, EventArgs e)
  512.         {
  513.             try
  514.             {
  515.                 AppraisalList apprisal = grvappraisal.SelectedRows[0].DataBoundItem as AppraisalList;
  516.  
  517.                 if (grvappraisal.SelectedRows.Count == 0)
  518.                     return;
  519.                 grvappraisal.SelectionMode = Telerik.WinControls.UI.GridViewSelectionMode.FullRowSelect;
  520.  
  521.  
  522.                 DialogResult result = MessageBox.Show(string.Format("Are you sure you want to delete the following Appraisal record:     ", ""), "Delete Apprisal Record", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);
  523.  
  524.                 if (result == DialogResult.Yes)
  525.                 {
  526.                
  527.                      
  528.                     if (pamsContext.SaveChanges())
  529.                     {
  530.  
  531.  
  532.                         BindAppraisalList(false);
  533.  
  534.                     }
  535.                     else
  536.                         MessageBox.Show("The Appraisal record could not be deleted!", "Delete Appraisal Record", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
  537.                 }
  538.             }
  539.             catch (Exception ex)
  540.             {
  541.                 ExceptionBox.Show("An error occurred in the delete Appraisal record button Click event.", ex);
  542.             }
  543.         }
  544.  
  545.      
  546.         }
  547.  
  548. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement