protected void GridView_Sorting(object sender, GridViewSortEventArgs e)
    {
        // dont allow sorting if in edit mode
        if (GrdLetterPrintHistory.EditIndex >= 0)
        {
            return;
        }

        DataTable dataTable = Session["letterbestprinthistory_data"] as DataTable;

        if (dataTable != null)
        {
            if (Session["letterbestprinthistory_sortexpression"] == null)
            {
                Session["letterbestprinthistory_sortexpression"] = "";
            }

            DataView dataView    = new DataView(dataTable);
            string[] sortData    = Session["letterbestprinthistory_sortexpression"].ToString().Trim().Split(' ');
            string   newSortExpr = (e.SortExpression == sortData[0] && sortData[1] == "ASC") ? "DESC" : "ASC";
            dataView.Sort = e.SortExpression + " " + newSortExpr;
            Session["letterbestprinthistory_sortexpression"] = e.SortExpression + " " + newSortExpr;

            GrdLetterPrintHistory.DataSource = dataView;
            GrdLetterPrintHistory.DataBind();
        }
    }
    protected void FillGrid()
    {
        int patient_id = IsValidFormPatient() ? GetFormPatient(false) : -1;

        if (patient_id != -1)
        {
            Patient patient = PatientDB.GetByID(patient_id);
            Page.Title              = lblHeading.Text = "B.E.S.T. Letter Print History For ";
            lnkToEntity.Text        = patient.Person.FullnameWithoutMiddlename;
            lnkToEntity.NavigateUrl = "PatientDetailV2.aspx?type=view&id=" + patient.PatientID;
        }


        DataTable dt = LetterBestPrintHistoryDB.GetDataTable(patient_id);

        Session["letterbestprinthistory_data"] = dt;

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


            try
            {
                GrdLetterPrintHistory.DataBind();
            }
            catch (Exception ex)
            {
                HideTableAndSetErrorMessage("", ex.ToString());
            }
        }
        else
        {
            dt.Rows.Add(dt.NewRow());
            GrdLetterPrintHistory.DataSource = dt;
            GrdLetterPrintHistory.DataBind();

            int TotalColumns = GrdLetterPrintHistory.Rows[0].Cells.Count;
            GrdLetterPrintHistory.Rows[0].Cells.Clear();
            GrdLetterPrintHistory.Rows[0].Cells.Add(new TableCell());
            GrdLetterPrintHistory.Rows[0].Cells[0].ColumnSpan = TotalColumns;
            GrdLetterPrintHistory.Rows[0].Cells[0].Text       = "No Record Found";
        }
    }
    protected void FillGrid()
    {
        int org_id               = IsValidFormOrganisation() ? GetFormOrganisation(false) :  0;
        int patient_id           = IsValidFormPatient()      ? GetFormPatient(false)      : -1;
        int register_referrer_id = IsValidFormRegReferrer()  ? GetFormRegReferrer(false)  : -1;


        if (patient_id != -1)
        {
            Patient patient = PatientDB.GetByID(patient_id);
            lblHeading.Text         = "Letter Print History For ";
            lnkToEntity.Text        = patient.Person.FullnameWithoutMiddlename;
            lnkToEntity.NavigateUrl = "PatientDetailV2.aspx?type=view&id=" + patient.PatientID;
        }
        else if (org_id != 0)
        {
            Organisation org = OrganisationDB.GetByID(org_id);
            lblHeading.Text         = "Letter Print History For ";
            lnkToEntity.Text        = org.Name;
            lnkToEntity.NavigateUrl = "OrganisationDetailV2.aspx?type=view&id=" + org.OrganisationID;
        }


        DataTable dt = LetterPrintHistoryDB.GetDataTable(DateTime.MinValue, DateTime.MinValue, org_id, patient_id, register_referrer_id, Convert.ToInt32(Session["SiteID"]));

        Session["letterprinthistory_data"] = dt;

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


            try
            {
                GrdLetterPrintHistory.DataBind();
                GrdLetterPrintHistory.PagerSettings.FirstPageText = "1";
                GrdLetterPrintHistory.PagerSettings.LastPageText  = GrdLetterPrintHistory.PageCount.ToString();
                GrdLetterPrintHistory.DataBind();
            }
            catch (Exception ex)
            {
                SetErrorMessage("", ex.ToString());
            }
        }
        else
        {
            dt.Rows.Add(dt.NewRow());
            GrdLetterPrintHistory.DataSource = dt;
            GrdLetterPrintHistory.DataBind();

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