protected void FillPatientGrid()
    {
        UserView userView = UserView.GetInstance();

        lblHeading.Text = !userView.IsAgedCareView ? "Patients" : "Residents";


        int    regRefID = IsValidFormRef() ? GetFormRef(false) : -1;
        int    orgID    = IsValidFormOrg() ? GetFormOrg(false) : 0;
        string orgIDs   = orgID != 0 ? orgID.ToString() : (IsValidFormOrgs() ? GetFormOrgs(false) : string.Empty);


        DataTable dt = null;

        if (regRefID != -1)
        {
            dt = PatientReferrerDB.GetDataTable_PatientsOf(regRefID, false, false, userView.IsClinicView, userView.IsGPView, txtSearchSurname.Text.Trim(), chkSurnameSearchOnlyStartWith.Checked);
        }
        else if (orgIDs != string.Empty)
        {
            dt = RegisterPatientDB.GetDataTable_PatientsOf(false, orgIDs, false, false, userView.IsClinicView, userView.IsGPView, txtSearchSurname.Text.Trim(), chkSurnameSearchOnlyStartWith.Checked);
        }
        else
        {
            dt = PatientDB.GetDataTable(false, false, userView.IsClinicView, userView.IsGPView, txtSearchSurname.Text.Trim(), chkSurnameSearchOnlyStartWith.Checked);
        }

        // update AjaxLivePatientSurnameSearch and PatientListV2.aspx and PatientListPopup to disallow providers to see other patients.
        if (userView.IsProviderView)  // remove any patients who they haven't had bookings with before
        {
            Patient[] patients = BookingDB.GetPatientsOfBookingsWithProviderAtOrg(Convert.ToInt32(Session["StaffID"]), Convert.ToInt32(Session["OrgID"]));
            System.Collections.Hashtable hash = new System.Collections.Hashtable();
            foreach (Patient p in patients)
            {
                hash[p.PatientID] = 1;
            }

            for (int i = dt.Rows.Count - 1; i >= 0; i--)
            {
                if (hash[Convert.ToInt32(dt.Rows[i]["patient_id"])] == null)
                {
                    dt.Rows.RemoveAt(i);
                }
            }
        }

        Session["patientinfo_data"] = dt;

        if (dt.Rows.Count > 0)
        {
            if (IsPostBack && Session["patientinfo_sortexpression"] != null && Session["patientinfo_sortexpression"].ToString().Length > 0)
            {
                DataView dataView = new DataView(dt);
                dataView.Sort         = Session["patientinfo_sortexpression"].ToString();
                GrdPatient.DataSource = dataView;
            }
            else
            {
                GrdPatient.DataSource = dt;
            }

            try
            {
                GrdPatient.DataBind();
                GrdPatient.PagerSettings.FirstPageText = "1";
                GrdPatient.PagerSettings.LastPageText  = GrdPatient.PageCount.ToString();
                GrdPatient.DataBind();
            }
            catch (Exception ex)
            {
                this.lblErrorMessage.Visible = true;
                this.lblErrorMessage.Text    = ex.ToString();
            }
        }
        else
        {
            dt.Rows.Add(dt.NewRow());
            GrdPatient.DataSource = dt;
            GrdPatient.DataBind();

            int TotalColumns = GrdPatient.Rows[0].Cells.Count;
            GrdPatient.Rows[0].Cells.Clear();
            GrdPatient.Rows[0].Cells.Add(new TableCell());
            GrdPatient.Rows[0].Cells[0].ColumnSpan = TotalColumns;
            GrdPatient.Rows[0].Cells[0].Text       = "No Record Found";
        }
    }