///<summary>Fills the big main grid.</summary>
        private void FillGridMobileAppDevices()
        {
            gridMobileAppDevices.BeginUpdate();
            //Columns
            gridMobileAppDevices.ListGridColumns.Clear();
            gridMobileAppDevices.AddColumn("Device Name", 0);
            gridMobileAppDevices.AddColumn("Last Attempt", 0);
            gridMobileAppDevices.AddColumn("Last Login", 0);
            if (PrefC.HasClinicsEnabled)
            {
                gridMobileAppDevices.AddColumn("Clinic", 0);
            }
            gridMobileAppDevices.ListGridColumns.Add(new GridColumn("Enabled", 50, HorizontalAlignment.Center));
            if (_eClipboardAllowEdit)
            {
                gridMobileAppDevices.ListGridColumns.Add(new GridColumn("Delete", 45, HorizontalAlignment.Center));
            }
            //Rows
            gridMobileAppDevices.ListGridRows.Clear();
            List <MobileAppDevice> listDevicesToShow = MobileAppDevices.GetForUser(Security.CurUser);

            if (_clinicNumEClipboardTab > 0)
            {
                listDevicesToShow.RemoveAll(x => x.ClinicNum != _clinicNumEClipboardTab);
            }
            foreach (MobileAppDevice device in listDevicesToShow)
            {
                GridRow row = new GridRow();
                row.Cells.Add(device.DeviceName + "\r\n(" + device.UniqueID + ")");
                row.Cells.Add((device.LastAttempt.Year > 1880 ? device.LastAttempt.ToString() : ""));
                row.Cells.Add((device.LastLogin.Year > 1880 ? device.LastLogin.ToString() : ""));
                if (PrefC.HasClinicsEnabled)
                {
                    row.Cells.Add((device.ClinicNum == 0 ? Clinics.GetPracticeAsClinicZero() : Clinics.GetClinic(device.ClinicNum)).Abbr);
                }
                row.Cells.Add((device.IsAllowed ? "X" : ""));
                if (_eClipboardAllowEdit)
                {
                    #region Delete click handler
                    void DeleteClick(object sender, EventArgs e)
                    {
                        if (device.PatNum > 0)
                        {
                            MsgBox.Show("A patient is currently using this device. Please clear the patient from the device using the Kiosk Manager" +
                                        " or wait until the patient is no longer using the device.");
                            return;
                        }
                        if (!MsgBox.Show(MsgBoxButtons.YesNo, "This will immediately remove the device from the database and all other workstations." +
                                         " Continue?"))
                        {
                            return;
                        }
                        MobileAppDevices.Delete(device.MobileAppDeviceNum);
                        FillGridMobileAppDevices();
                    }

                    #endregion Delete click handler
                    GridCell cell = new GridCell("Delete");
                    cell.ColorBackG = Color.LightGray;
                    cell.ClickEvent = DeleteClick;
                    row.Cells.Add(cell);
                }
                row.Tag = device;
                gridMobileAppDevices.ListGridRows.Add(row);
            }
            gridMobileAppDevices.EndUpdate();
        }
        private void FillGrid()
        {
            SheetDevice selected = new SheetDevice(new MobileAppDevice());          //just instantiate to something random that won't match any of our actual devices

            if (gridMain.GetSelectedIndex() > -1)
            {
                selected = (SheetDevice)gridMain.ListGridRows[gridMain.GetSelectedIndex()].Tag;
            }
            List <SheetDevice> listDevices = new List <SheetDevice>();

            foreach (TerminalActive kiosk in TerminalActives.Refresh())
            {
                listDevices.Add(new SheetDevice(kiosk));
            }
            List <MobileAppDevice> listMobileDevices = new List <MobileAppDevice>();

            if (PrefC.HasClinicsEnabled)
            {
                //Option "All" is selected and at least one clinic is signed up for the eClipboard feature
                if (contrClinicPicker.IsAllSelected && PrefC.GetString(PrefName.EClipboardClinicsSignedUp) != "")
                {
                    listMobileDevices = MobileAppDevices.GetForUser(Security.CurUser).FindAll(x => x.IsAllowed);
                }
                //A specific clinic is selected and that is signed up for the eClipboard feature
                else if (MobileAppDevices.IsClinicSignedUpForEClipboard(contrClinicPicker.SelectedClinicNum))
                {
                    listMobileDevices = MobileAppDevices.GetForUser(Security.CurUser).FindAll(x => x.IsAllowed && x.ClinicNum == contrClinicPicker.SelectedClinicNum);
                }
            }
            //We aren't using clinics and the zero clinic is signed up
            else if (MobileAppDevices.IsClinicSignedUpForEClipboard(0))
            {
                listMobileDevices = MobileAppDevices.GetForUser(Security.CurUser).FindAll(x => x.IsAllowed);
            }
            //Add the clinics we decided on the the d
            foreach (MobileAppDevice device in listMobileDevices)
            {
                listDevices.Add(new SheetDevice(device));
            }
            int selectedIndex = -1;

            gridMain.BeginUpdate();
            gridMain.ListGridColumns.Clear();
            gridMain.AddColumn("Device Name", 150);
            gridMain.AddColumn("Session Name", 110);
            gridMain.AddColumn("Patient", 185);
            if (PrefC.HasClinicsEnabled)
            {
                gridMain.AddColumn("Clinic", 150);
            }
            gridMain.ListGridColumns.Add(new GridColumn("Action", 50)
            {
                TextAlign = HorizontalAlignment.Center
            });
            if (_isSetupMode)
            {
                gridMain.ListGridColumns.Add(new GridColumn("Delete", 50)
                {
                    TextAlign = HorizontalAlignment.Center
                });
            }
            gridMain.ListGridRows.Clear();
            foreach (SheetDevice device in listDevices)
            {
                GridRow row = new GridRow();
                row.Tag = device;
                if (device.IsMobileAppDevice)
                {
                    row.Cells.Add(new GridCell(device.Name + "\r\n(" + device.MobileDevice.UniqueID + ")"));
                }
                else
                {
                    row.Cells.Add(new GridCell(device.Name));
                }
                row.Cells.Add(new GridCell(device.SessionName));
                row.Cells.Add(new GridCell(device.PatName));
                if (PrefC.HasClinicsEnabled)
                {
                    row.Cells.Add(new GridCell(device.ClinicDesc));
                }
                #region Load/Clear click handler
                void CellClick(object sender, EventArgs e)
                {
                    FillGrid();
                    if (device.PatNum == 0)                    //we are trying to load the patient
                    {
                        if (FormOpenDental.CurPatNum == 0)
                        {
                            MsgBox.Show(this, "There is currently no patient selected to send to the device. Select a patient in Open Dental " +
                                        "in order to continue.");
                            return;
                        }
                        if (device.IsKiosk)                          //kiosk only
                        {
                            if (listSheets.Items.Count == 0)         //eClipboard will allow to continue to load here in case we just want to take a photo
                            {
                                MsgBox.Show(this, "There are no sheets to send to the computer or device for the current patient.");
                                return;
                            }
                        }
                        else                           //eclipboard only
                        {
                            if (MobileAppDevices.PatientIsAlreadyUsingDevice(FormOpenDental.CurPatNum))
                            {
                                MsgBox.Show(this, "The patient you have selected is already using another device. Select a patient who is not currently " +
                                            "using a device in order to continue.");
                                return;
                            }
                            Appointment apptForToday = Appointments.GetAppointmentsForPat(FormOpenDental.CurPatNum).FirstOrDefault(x => x.AptDateTime.Date == DateTime.Today.Date);
                            if (apptForToday == null)
                            {
                                MsgBox.Show(this, "The patient you have selected does not have an appointment today. Only patients with an " +
                                            "appointment on the same day can be sent to eClipboard.");
                                return;
                            }
                            List <Sheet> listNonMobileSheets = listSheets.AllTags <Sheet>().FindAll(x => !x.HasMobileLayout);
                            if (listNonMobileSheets.Count > 0)
                            {
                                if (!MsgBox.Show(MsgBoxButtons.YesNo, "The following sheets that have been queued for this patient cannot be " +
                                                 $"loaded onto an eClipboard device because they do not have a mobile layout: \r\n" +
                                                 $"{string.Join(", ",listNonMobileSheets.Select(x => x.Description))}. \r\nDo you still wish to continue?"))
                                {
                                    return;
                                }
                            }
                            //They are in setup mode (not normal workflow) and there are no sheets for this patient. They have not run the rules to generate
                            //sheets as the patient has not been marked as arrived. When they push the patient to the device, they will not generate the sheets
                            //from there either. Ask them if they want to generate the sheets in this case.
                            if (_isSetupMode && listSheets.Items.Count == 0 && ClinicPrefs.GetBool(PrefName.EClipboardCreateMissingFormsOnCheckIn, apptForToday.ClinicNum))
                            {
                                bool generateSheets = MsgBox.Show(MsgBoxButtons.YesNo, "This patient has no forms to load. Would you like to generate the " +
                                                                  "forms based on the eClipboard rules?");
                                if (generateSheets)
                                {
                                    //We do not need to update the UI here. It will be updated at the end of this click method.
                                    Sheets.CreateSheetsForCheckIn(apptForToday);
                                }
                            }
                        }
                        device.SetPatNum(FormOpenDental.CurPatNum);
                    }
                    else                       //we are trying to clear the patient
                    {
                        if (!MsgBox.Show(this, true, "A patient is currently using the terminal.  If you continue, they will lose the information that is on their "
                                         + "screen.  Continue anyway?"))
                        {
                            return;
                        }
                        device.SetPatNum(0);
                    }
                    FillGrid();
                }

                #endregion Load/Clear click handler
                GridCell cell = new GridCell(device.PatNum == 0?"Load":"Clear");
                cell.ColorBackG = Color.LightGray;
                cell.ClickEvent = CellClick;
                row.Cells.Add(cell);
                if (_isSetupMode)
                {
                    #region Delete click handler
                    void DeleteClick(object sender, EventArgs e)
                    {
                        FillGrid();
                        if (!MsgBox.Show(this, MsgBoxButtons.OKCancel, "A row should not be deleted unless it is showing erroneously and there really is " +
                                         "nothing running on the computer or device shown.  Continue anyway?"))
                        {
                            return;
                        }
                        device.Delete();
                        FillGrid();
                    }

                    #endregion Delete click handler
                    cell            = new GridCell("Delete");
                    cell.ColorBackG = Color.LightGray;
                    cell.ClickEvent = DeleteClick;
                    row.Cells.Add(cell);
                }
                gridMain.ListGridRows.Add(row);
                if (selected != null && device.Matches(selected))
                {
                    selectedIndex = gridMain.ListGridRows.Count - 1;
                }
            }
            gridMain.EndUpdate();
            gridMain.SetSelected(selectedIndex, true);           //selectedIndex could be -1 if the selected term is not in the list, default to row 0
            FillPat();
        }