Tuesday, January 27, 2015

All date related solution

All date related solution
  protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GetnextSunday();

                for (int i = 1; i <= 31; i++)
                {
                    DateTime start = new DateTime(2015, 1, i);
                    DateTime stop = new DateTime(2015, 1, 31);

                    int totalWorkingDays = GetNumberOfWorkingDays(start, stop);

                    Console.WriteLine("There are {1} working days from Oct {0}, 2014 to Oct 31, 2014.", i, totalWorkingDays);
                }
                DateTime start1 = new DateTime(2015, 1, 1);
                DateTime stop1 = new DateTime(2015, 1, 31);
                int cntcoun = CountDays(start1, stop1);
                GetAllSundays(start1, stop1);
            }
        }
        public void GetnextSunday()
        {
            var date = DateTime.Now;
            var nextSunday1 = date.AddDays(7 - (int)date.DayOfWeek);

            DateTime d = DateTime.Today;

            int offset = d.DayOfWeek - DayOfWeek.Monday;

            DateTime lastMonday = d.AddDays(-offset);
            DateTime nextSunday = lastMonday.AddDays(6);
        }
        private static int GetNumberOfWorkingDays(DateTime start, DateTime stop)
        {
            TimeSpan interval = stop - start;

            int totalWeek = interval.Days / 7;
            int totalWorkingDays = 5 * totalWeek;

            int remainingDays = interval.Days % 7;


            for (int i = 1; i <= remainingDays; i++)
            {
                DayOfWeek test = (DayOfWeek)(((int)start.DayOfWeek) % 7);
                if (test >= DayOfWeek.Monday && test <= DayOfWeek.Friday)
                    totalWorkingDays++;
            }

            return totalWorkingDays;
        }

        public int CountDays(DateTime fromDate, DateTime toDate)
        {
            int noOfDays = 0;
            DateTime fDate = Convert.ToDateTime(fromDate);
            DateTime tDate = Convert.ToDateTime(toDate);
            while (DateTime.Compare(fDate, tDate) <= 0)
            {
                if (fDate.DayOfWeek != DayOfWeek.Saturday && fDate.DayOfWeek != DayOfWeek.Sunday)
                {
                    noOfDays += 1;
                }
                fDate = fDate.AddDays(1);
            }
            return noOfDays;
        }
        public void GetAllSundays(DateTime Date1, DateTime Date2)
        {
            TimeSpan DateDiff = Date2.Subtract(Date1);
            List<DateTime> lstdate = new List<DateTime>();
            for (int i = 0; i <= DateDiff.Days; i++)
            {
                if (Date1.Date.AddDays(i).DayOfWeek == DayOfWeek.Sunday)
                {
                    lstdate.Add(Date1.Date.AddDays(i));
                }
            }
        }

Read more »

how to do sorting on gridview


 private void BindGrid()
        {
            if (ViewState["SortExp"] != null && ViewState["order"] != null)
            {
                lstemp.Sort(new GenericComparer<Employee>(ViewState["SortExp"].ToString(), (SortDirection)ViewState["order"]));
            }
            GridView1.DataSource = lstemp;
            GridView1.DataBind();

        }
        SortDirection sortOrder;
        protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                if (ViewState["SortExp"] != null && ViewState["SortExp"].ToString().ToLower() == e.SortExpression.ToString().ToLower()
                        && ViewState["order"] != null && (SortDirection)ViewState["order"] == SortDirection.Ascending)
                {
                    sortOrder = SortDirection.Descending;
                }
                else
                {
                    sortOrder = SortDirection.Ascending;
                }

                ViewState["order"] = sortOrder;
                ViewState["SortExp"] = e.SortExpression;
                this.BindGrid();
            }
            catch (Exception ex)
            {
               
            }
        }

create new class

public class GenericComparer<T> : IComparer<T>
    {
        /// <summary>
        /// Define sort direction Ascending or descending
        /// </summary>
        private SortDirection sortDirection;

        /// <summary>
        /// Gets or sets the sort direction.
        /// </summary>
        /// <value>The sort direction.</value>
        /// <remarks></remarks>
        public SortDirection SortDirection
        {
            get { return this.sortDirection; }
            set { this.sortDirection = value; }
        }

        /// <summary>
        /// Define the name in which data is to be sort.
        /// </summary>
        private string sortExpression;

        /// <summary>
        /// Initializes a new instance of the <see cref="GenericComparer&lt;T&gt;"/> class.
        /// </summary>
        /// <param name="sortExpression">The sort expression.</param>
        /// <param name="sortDirection">The sort direction.</param>
        /// <remarks></remarks>
        public GenericComparer(string sortExpression, SortDirection sortDirection)
        {
            this.sortExpression = sortExpression;
            this.sortDirection = sortDirection;
        }
        public int Compare(T x, T y)
        {
            PropertyInfo propertyInfo = typeof(T).GetProperty(sortExpression);
            IComparable obj1 = (IComparable)propertyInfo.GetValue(x, null);
            IComparable obj2 = (IComparable)propertyInfo.GetValue(y, null);

            if (obj1 == null)
            {
                if (SortDirection == SortDirection.Ascending)
                {
                    return (obj2 == null) ? 0 : -1;
                }
                else
                {
                    return (obj2 == null) ? -1 : 0;
                }
            }
            if (obj2 == null) { return 1; }

            if (SortDirection == SortDirection.Ascending)
            {
                return obj1.CompareTo(obj2);
            }
            else return obj2.CompareTo(obj1);
        }
     
    }

Read more »

Followers