private void BindGridData(GridSortCommandEventArgs sortEventArgs)
    {
        InvoicesRepository repo = new InvoicesRepository();
        IList<Invoices> list = new List<Invoices>();
        if (!string.IsNullOrEmpty(Request.QueryString["type"]) && Request.QueryString["type"] == "search")
        {
            int pageNumber = gridInvoice.CurrentPageIndex + 1;
            string sortExpress = string.Empty;
            string sortExpressInvert = string.Empty;
            foreach (GridSortExpression item in gridInvoice.MasterTableView.SortExpressions)
            {
                GridSortOrder newSortOrder = item.SortOrder;
                if (sortEventArgs != null && item.FieldName == sortEventArgs.SortExpression)
                {
                    newSortOrder = sortEventArgs.NewSortOrder;
                }

                if (!string.IsNullOrEmpty(sortExpress) && newSortOrder != GridSortOrder.None)
                {
                    sortExpress += ", ";
                    sortExpressInvert += ", ";
                }
                if (newSortOrder == GridSortOrder.Ascending)
                {
                    sortExpress += item.FieldName + " ASC";
                    sortExpressInvert += item.FieldName + " DESC";
                }
                else if (newSortOrder == GridSortOrder.Descending)
                {
                    sortExpress += item.FieldName + " DESC";
                    sortExpressInvert += item.FieldName + " ASC";
                }
            }

            if (sortEventArgs != null && !sortExpress.Contains(sortEventArgs.SortExpression))
            {
                if (!string.IsNullOrEmpty(sortExpress) && sortEventArgs.NewSortOrder != GridSortOrder.None)
                {
                    sortExpress += ", ";
                    sortExpressInvert += ", ";
                }
                if (sortEventArgs.NewSortOrder == GridSortOrder.Ascending)
                {
                    sortExpress += sortEventArgs.SortExpression + " ASC";
                    sortExpressInvert += sortEventArgs.SortExpression + " DESC";
                }
                else if (sortEventArgs.NewSortOrder == GridSortOrder.Descending)
                {
                    sortExpress += sortEventArgs.SortExpression + " DESC";
                    sortExpressInvert += sortEventArgs.SortExpression + " ASC";
                }
            }

            if (!string.IsNullOrEmpty(sortExpress))
            {
                if (sortExpress.Contains("CompanyName"))
                {
                    sortExpress = sortExpress.Replace("CompanyName", "SocNom");
                    sortExpressInvert = sortExpressInvert.Replace("CompanyName", "SocNom");
                }
                if (sortExpress.Contains("CompanyId"))
                {
                    sortExpress = sortExpress.Replace("CompanyId", "SocieteID");
                    sortExpressInvert = sortExpressInvert.Replace("CompanyId", "SocieteID");
                }
                if (sortExpress.Contains("InvoiceDate"))
                {
                    sortExpress = sortExpress.Replace("InvoiceDate", "Date");
                    sortExpressInvert = sortExpressInvert.Replace("InvoiceDate", "Date");
                }
            }
            else
            {
                sortExpress = "IdYear DESC, IdFactNumber DESC, IdTypeInvoice ASC";
                sortExpressInvert = "IdYear ASC, IdFactNumber ASC, IdTypeInvoice DESC";
            }

            InvoicesSearchCriteria criteria = GetSearchCriteria();

            if (!string.IsNullOrEmpty(Request.QueryString["invoiceNumberFrom"]))
                criteria.InvoiceNumberFrom = int.Parse(Request.QueryString["invoiceNumberFrom"]);
            if (!string.IsNullOrEmpty(Request.QueryString["invoiceNumberTo"]))
                criteria.InvoiceNumberTo = int.Parse(Request.QueryString["invoiceNumberTo"]);

            if (!string.IsNullOrEmpty(Request.QueryString["fiscalYear"]))
                criteria.FiscalYear = int.Parse(Request.QueryString["fiscalYear"]);

            if (!string.IsNullOrEmpty(Request.QueryString["dateFrom"]))
                criteria.InvoiceDateFrom = DateTime.ParseExact(Request.QueryString["dateFrom"], "dd/MM/yyyy",
                    System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat);
            if (!string.IsNullOrEmpty(Request.QueryString["dateTo"]))
                criteria.InvoiceDateTo = DateTime.ParseExact(Request.QueryString["dateTo"], "dd/MM/yyyy",
                    System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat);

            if (!string.IsNullOrEmpty(Request.QueryString["invoiceType"]))
                criteria.InvoiceType = Request.QueryString["invoiceType"];

            if (!string.IsNullOrEmpty(Request.QueryString["customer"]))
                criteria.Customer = int.Parse(Request.QueryString["customer"]);

            gridInvoice.VirtualItemCount = repo.CountInvoices(criteria, pageSize, pageNumber, sortExpress, sortExpressInvert);
            list = repo.SearchInvoices(criteria, pageSize, pageNumber, sortExpress, sortExpressInvert);
        }
        else if (!string.IsNullOrEmpty(Request.QueryString["type"]) && Request.QueryString["type"] == "future")
        {
            lblTitle.Text = ResourceManager.GetString("lblInvoiceFutureGridHeader");
            list = repo.GetFutureInvoices(int.Parse(WebConfig.FirstNumberFutureInvoice));

        }
        else if (!string.IsNullOrEmpty(Request.QueryString["CompanyId"]))
        {
            int companyID = int.Parse(Request.QueryString["CompanyId"]);
            list = repo.GetUnpaidInvoicesOfCompany(companyID, false);
        }
        else
        {
            int notConfirmedFuture = repo.CountNotConfirmedFutureInvoices(int.Parse(WebConfig.FirstNumberFutureInvoice));
            if (notConfirmedFuture > 0)
            {
                Response.Redirect("InvoicesPage.aspx?type=future");
            }
        }

        gridInvoice.DataSource = list;
        btnPrintAll.Visible = (list.Count > 0);
        btnExcelExport.Visible = (list.Count > 0);
        //btnPrintSelection.Visible = false;
    }
    protected void OnButtonInvoicePrintAllClicked(object sender, EventArgs e)
    {
        List<InvoicingFile> fileList = new List<InvoicingFile>();

        if (!string.IsNullOrEmpty(Request.QueryString["type"]) && Request.QueryString["type"] == "search")
        {
            InvoicesSearchCriteria criteria = new InvoicesSearchCriteria();
            string sortExpress = "IdFactNumber DESC, IdTypeInvoice ASC, IdYear DESC";
            string sortExpressInvert = "IdFactNumber ASC, IdTypeInvoice DESC, IdYear ASC";

            if (!string.IsNullOrEmpty(Request.QueryString["invoiceNumberFrom"]))
                criteria.InvoiceNumberFrom = int.Parse(Request.QueryString["invoiceNumberFrom"]);
            if (!string.IsNullOrEmpty(Request.QueryString["invoiceNumberTo"]))
                criteria.InvoiceNumberTo = int.Parse(Request.QueryString["invoiceNumberTo"]);

            if (!string.IsNullOrEmpty(Request.QueryString["fiscalYear"]))
                criteria.FiscalYear = int.Parse(Request.QueryString["fiscalYear"]);

            if (!string.IsNullOrEmpty(Request.QueryString["dateFrom"]))
                criteria.InvoiceDateFrom = DateTime.ParseExact(Request.QueryString["dateFrom"], "dd/MM/yyyy",
                    System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat);
            if (!string.IsNullOrEmpty(Request.QueryString["dateTo"]))
                criteria.InvoiceDateTo = DateTime.ParseExact(Request.QueryString["dateTo"], "dd/MM/yyyy",
                    System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat);

            if (!string.IsNullOrEmpty(Request.QueryString["invoiceType"]))
                criteria.InvoiceType = Request.QueryString["invoiceType"];

            if (!string.IsNullOrEmpty(Request.QueryString["customer"]))
                criteria.Customer = int.Parse(Request.QueryString["customer"]);

            InvoicesRepository repo = new InvoicesRepository();
            gridInvoice.VirtualItemCount = repo.CountInvoices(criteria, 10, 1, sortExpress, sortExpressInvert);
            IList<Invoices> list = repo.SearchInvoices(criteria, gridInvoice.VirtualItemCount,
                1, sortExpress, sortExpressInvert);
            foreach (Invoices item in list)
            {
                string filePath = Common.ExportInvoices(item, WebConfig.AddressFillInInvoice,
                    WebConfig.AbsoluteExportDirectory);

                InvoicingFile file = new InvoicingFile();
                file.InvoiceIdPK = item.InvoiceIdPK;
                file.FilePath = filePath;

                fileList.Add(file);
            }
            InvoicingMView.ActiveViewIndex = 1;
            ProcessPrintedFiles(fileList);

            /*string message = ResourceManager.GetString("messageExportSuccessfully");
            string script1 = "<script type=\"text/javascript\">";
            script1 += " alert(\"" + message + "\")";
            script1 += " </script>";

            if (!ClientScript.IsClientScriptBlockRegistered("redirectUser"))
                ClientScript.RegisterStartupScript(this.GetType(), "redirectUser", script1);*/
        }
    }