protected void GrdRegistration_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        Organisation org = OrganisationDB.GetByID(GetFormID());

        if (org == null)
        {
            HideTableAndSetErrorMessage("", "Invalid URL Parameters");
            return;
        }

        DataTable dt       = Session["registerpatienttoorg_data"] as DataTable;
        bool      tblEmpty = (dt.Rows.Count == 1 && dt.Rows[0][0] == DBNull.Value);

        if (!tblEmpty && e.Row.RowType == DataControlRowType.DataRow)
        {
            Label     lblId     = (Label)e.Row.FindControl("lblId");
            DataRow[] foundRows = dt.Select("register_patient_id=" + lblId.Text);
            DataRow   thisRow   = foundRows[0];


            DropDownList ddlPatient = (DropDownList)e.Row.FindControl("ddlPatient");
            if (ddlPatient != null)
            {
                Patient[] incList_orig = RegisterPatientDB.GetPatientsOf(GetFormViewOnlyLast(), org.OrganisationID);
                Patient[] incList      = Patient.RemoveByID(incList_orig, Convert.ToInt32(thisRow["patient_id"]));
                DataTable patient      = PatientDB.GetDataTable_AllNotInc(incList);
                patient.DefaultView.Sort = "surname ASC";
                foreach (DataRowView row in patient.DefaultView)
                {
                    ddlPatient.Items.Add(new ListItem(row["surname"].ToString() + ", " + row["firstname"].ToString() + " " + row["middlename"].ToString(), row["patient_id"].ToString()));
                }
                ddlPatient.SelectedValue = thisRow["patient_id"].ToString();
            }

            Label lblEPCExpiry     = (Label)e.Row.FindControl("lblEPCExpiry");
            Label lblEPCsRemaining = (Label)e.Row.FindControl("lblEPCsRemaining");
            if (lblEPCExpiry != null && lblEPCsRemaining != null)
            {
                if (!Convert.ToBoolean(thisRow["has_valid_epc"]))
                {
                    lblEPCExpiry.ForeColor     = System.Drawing.Color.Red;
                    lblEPCsRemaining.ForeColor = System.Drawing.Color.Red;
                }
            }

            HyperLink lnkBookings = (HyperLink)e.Row.FindControl("lnkBookings");
            if (lnkBookings != null)
            {
                lnkBookings.NavigateUrl = string.Format("~/BookingsV2.aspx?orgs={0}&patient={1}", org.OrganisationID, Convert.ToInt32(thisRow["patient_id"]));
            }

            Label lnkPatient = (Label)e.Row.FindControl("lnkPatient");
            if (lnkPatient != null)
            {
                string URL = "PatientDetailV2.aspx?type=view&id=" + Convert.ToInt32(thisRow["patient_id"]);
                if (URL.StartsWith("~"))
                {
                    URL = URL.Substring(1);
                }
                lnkPatient.Text = "<a href=\"#\" onclick=\"var win=window.open('" + URL + "', '_blank'); win.focus();return false;\" >" + thisRow["firstname"] + " " + thisRow["surname"] + "</a>";
            }


            Utilities.AddConfirmationBox(e);
            if ((e.Row.RowState & DataControlRowState.Edit) > 0)
            {
                Utilities.SetEditRowBackColour(e, System.Drawing.Color.LightGoldenrodYellow);
            }
        }
        if (e.Row.RowType == DataControlRowType.Footer && GrdRegistration.ShowFooter)
        {
            DropDownList ddlPatient = (DropDownList)e.Row.FindControl("ddlNewPatient");
            if (ddlPatient != null)
            {
                Patient[] incList = RegisterPatientDB.GetPatientsOf(GetFormViewOnlyLast(), org.OrganisationID);
                DataTable patient = PatientDB.GetDataTable_AllNotInc(incList);
                patient.DefaultView.Sort = "surname ASC";
                foreach (DataRowView row in patient.DefaultView)
                {
                    ddlPatient.Items.Add(new ListItem(row["surname"].ToString() + ", " + row["firstname"].ToString() + " " + row["middlename"].ToString(), row["patient_id"].ToString()));
                }

                if (patient.Rows.Count == 0)
                {
                    hideFotter = true;
                }
            }
        }
    }