private void ReBindGrid()
 {
     if (Session["WeeklyProductionCapacity"] != null)
     {
         RadGridProductionCapacities.DataSource = (List <WeeklyProductionCapacityBO>)Session["WeeklyProductionCapacity"];
         RadGridProductionCapacities.DataBind();
     }
 }
        private void PopulateDataGrid()
        {
            // Hide Controls
            dvEmptyContent.Visible = false;
            dvDataContent.Visible  = false;

            btnNewProductionCapacity.Visible = true;

            // get the first monday of the week
            var daysTillMonday = (int)DateTime.Today.DayOfWeek - (int)DayOfWeek.Monday;
            var monday         = DateTime.Today.AddDays(-daysTillMonday);

            // get the first monday  of specific month
            DateTime dt = DateTime.Now;

            if (ddlMonth.SelectedIndex > -1 && ddlYear.SelectedIndex > -1)
            {
                dt = new DateTime(int.Parse(ddlYear.SelectedValue), int.Parse(ddlMonth.SelectedValue), 1);
                dt = dt.AddDays(1);
            }

            // Search text
            string searchText = txtSearch.Text.ToLower().Trim();

            // Populate Item Attribute
            WeeklyProductionCapacityBO objProductionCapacity = new WeeklyProductionCapacityBO();

            // Sort by condition
            List <WeeklyProductionCapacityBO> lstProductionCapacity = new List <WeeklyProductionCapacityBO>();

            if ((searchText != string.Empty) && (searchText != "search"))
            {
                lstProductionCapacity = (from o in objProductionCapacity.SearchObjects().AsQueryable().Where(o => o.WeekendDate >= monday && o.WeekendDate.ToString().Contains(searchText))
                                         .OrderBy(SortExpression).ToList <WeeklyProductionCapacityBO>()
                                         select o).ToList();
            }
            else
            {
                if (ddlYear.SelectedIndex > -1)
                {
                    if (int.Parse(ddlMonth.SelectedValue) == DateTime.Now.Month)
                    {
                        lstProductionCapacity = objProductionCapacity.SearchObjects().AsQueryable().Where(o => o.WeekendDate >= monday && o.WeekendDate.Year >= int.Parse(ddlYear.SelectedItem.Text)).OrderBy(SortExpression).ToList <WeeklyProductionCapacityBO>();
                    }
                }
                else if (this.ddlYear.SelectedIndex == -1)
                {
                    lstProductionCapacity = objProductionCapacity.SearchObjects().AsQueryable().OrderBy(SortExpression).ToList <WeeklyProductionCapacityBO>();
                }
                else
                {
                    lstProductionCapacity = objProductionCapacity.SearchObjects().AsQueryable().Where(o => o.WeekendDate.Date >= monday).OrderBy(SortExpression).ToList <WeeklyProductionCapacityBO>();
                }
            }

            if (this.ddlMonth.SelectedIndex > -1)
            {
                if (int.Parse(this.ddlMonth.SelectedValue) != DateTime.Now.Month)
                {
                    lstProductionCapacity = objProductionCapacity.SearchObjects().AsQueryable().Where(o => o.WeekendDate >= dt).OrderBy(o => o.WeekendDate).ToList <WeeklyProductionCapacityBO>();
                }
            }
            else if (this.ddlMonth.SelectedIndex == -1)
            {
                lstProductionCapacity = lstProductionCapacity.ToList <WeeklyProductionCapacityBO>();
            }

            if (lstProductionCapacity.Count > 0)
            {
                RadGridProductionCapacities.AllowPaging = (lstProductionCapacity.Count > RadGridProductionCapacities.PageSize);
                RadGridProductionCapacities.DataSource  = lstProductionCapacity;
                RadGridProductionCapacities.DataBind();
                Session["WeeklyProductionCapacity"] = lstProductionCapacity;

                dvDataContent.Visible = true;
            }
            else if ((searchText != string.Empty && searchText != "search") || (ddlYear.SelectedIndex > -1) || (ddlMonth.SelectedIndex > -1))
            {
                lblSerchKey.Text = searchText + ((searchText != string.Empty) ? " - " : string.Empty);

                dvDataContent.Visible    = true;
                dvNoSearchResult.Visible = true;
            }
            else
            {
                dvEmptyContent.Visible           = true;
                btnNewProductionCapacity.Visible = true;
            }
            //change btnNewProductionCapacity button Text
            List <WeeklyProductionCapacityBO> lstWeeklyProdCap = (new WeeklyProductionCapacityBO()).SearchObjects();

            if (lstWeeklyProdCap.Count == 0)
            {
                btnNewProductionCapacity.InnerText = "Add weeks for year " + DateTime.Now.Year.ToString();
                lblDataAdded.Text = "Weeks until December " + DateTime.Now.Year.ToString();
            }
            if (lstWeeklyProdCap.Count > 0)
            {
                btnNewProductionCapacity.InnerText = "Add weeks for year " + lstWeeklyProdCap.Last().WeekendDate.AddYears(1).Year;
                lblDataAdded.Text = "Weeks until December " + lstWeeklyProdCap.Last().WeekendDate.Year.ToString();
            }

            RadGridProductionCapacities.Visible = (lstProductionCapacity.Count > 0);
        }