//Need to add and remove the grid to refresh the grid properly
 //otherwise the row to add the new record isn't displayed if there
 //was an error.
 private void RefreshGrid()
 {
     try
     {
         wrapperGrid.Children.Remove(contractorDataGrid);
         wrapperGrid.Children.Add(contractorDataGrid);
         contractorDataSetTableAdapter.Fill(contractorDataSet.Contractor);
     }
     catch (Exception ex)
     {
         ModernDialog.ShowMessage("Error loading data. " + ex.Message, "Warning", MessageBoxButton.OK, true);
     }
 }
        private void btnSearch_Click(object sender, RoutedEventArgs e)
        {
            ContractorDataSet contractorDataSet = new ContractorDataSet();

            ContractorDataSetTableAdapters.ContractorTableAdapter contractorDataSetTableAdapter = new ContractorDataSetTableAdapters.ContractorTableAdapter();
            try {
                contractorDataSetTableAdapter.Fill(contractorDataSet.Contractor);
            }
            catch (Exception ex)
            {
                ModernDialog.ShowMessage(ex.Message, "Error", MessageBoxButton.OK, true);
            }

            int searchedIrd = 0;

            if (int.TryParse(txtSearchedIrd.Text, out searchedIrd) == false)
            {
                Hide();
                ModernDialog.ShowMessage("Please enter a correct IRD number. It only consists 8 of numbers.", "Warning", MessageBoxButton.OK, true);
                return;
            }

            var searchResults = contractorDataSet.Contractor.Where(c => c.IRD == Convert.ToInt32(txtSearchedIrd.Text));

            if (searchResults.Count() == 1)
            {
                contractor = new Contractor(
                    searchResults.ElementAt(0).FirstName,
                    searchResults.ElementAt(0).LastName,
                    searchResults.ElementAt(0).IRD,
                    searchResults.ElementAt(0).IsMarried,
                    searchResults.ElementAt(0).NoChildren);

                //Note: Only implementing setting the display labels this way because of
                //the GetContractorInfo method provided in Contractor class.

                //Temporary variables to hold GetContractorInfo method output
                string fname      = "";
                string lname      = "";
                int    ird        = 0;
                bool   isMarried  = false;
                int    noChildren = 0;

                contractor.GetContractorInfo(out fname, out lname, out ird, out isMarried, out noChildren);

                // Setting labels from the temporary variables
                lblFname.Content         = fname;
                lblLname.Content         = lname;
                txtSearchedIrd.Text      = ird.ToString();
                checkIsMarried.IsChecked = isMarried;
                lblChildrenNo.Content    = noChildren;

                // making first section visible
                txtContractorInfo.Visibility = Visibility.Visible;
                pnlContractorInfo.Visibility = Visibility.Visible;
                pnlWorkHour.Visibility       = Visibility.Visible;
                txtHoursWorked.Focus(); //Set focus on next input to help the user.
                sp1.Visibility = Visibility.Visible;

                // making second section invisible and getting it ready for user input
                pnlResultLabels.Visibility = Visibility.Hidden;
                pnlResult.Visibility       = Visibility.Hidden;
                txtHoursWorked.Text        = "";

                btnCalculate.IsDefault = true;  //Make the next button default to allow easy input.
                btnSearch.IsDefault    = false;
            }
            else
            {
                Hide();
                ModernDialog.ShowMessage("Sorry, no match for this IRD number was found.", "Warning", MessageBoxButton.OK, true);
            }
        }