Create your own Calendar using GridView in ASP.Net

Hello Everyone,

Below are the code, how to generate your own calender using GridView. It's very simple and useful have a look on this.

Step:1 Write the below code in Calender.aspx page in your visual studio.



Today is <%=strCurrntMonthYear %>
Gridview alternatingrowstyle-backcolor="Aqua" autogeneratecolumns="false" cellpadding="10" cellspacing="5" headerstyle-horizontalalign="Center" id="gvCalander" rowstyle-backcolor="gray" rowstyle-horizontalalign="Center" runat="server" showheader="false">

boundfield datafield="AutoID" headertext="Days">
boundfield datafield="DaysName" headertext="Name">
boundfield datafield="Date" headertext="Name">
templatefield>

checkbox id="chkMark" runat="server">
asp:checkbox>




Step: 2 Write below code in Calender.aspx.cs page

public static string strCurrntMonthYear = "";

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //get current Year
            int Year = DateTime.Now.Year;

            //get current Month
            int inMonth = DateTime.Now.Month;

            //get Day's in current month
            int Days = DateTime.DaysInMonth(DateTime.Now.Year, DateTime.Now.Month);

            //Declare DataTable
            DataTable Dt = new DataTable("dtDays");

            //Declare Data Column
            DataColumn auto = new DataColumn("AutoID", typeof(System.Int32));
            Dt.Columns.Add(auto);

            DataColumn DaysName = new DataColumn("DaysName", typeof(string));
            Dt.Columns.Add(DaysName);

            DataColumn Date = new DataColumn("Date", typeof(string));
            Dt.Columns.Add(Date);

            //Declare Data Row
            DataRow dr = null;
            DateTime days;
            DateTime strDate;

            for (int i = 1; i <= Days; i++)
            {
                //Create row in DataTable
                dr = Dt.NewRow();
                days = new DateTime(Year, inMonth, i);  // find days name
                strDate = new DateTime(Year, inMonth, i); // find date w.r.t days

                dr["AutoID"] = i;
                dr["DaysName"] = days.DayOfWeek;
                dr["Date"] = strDate.Date.ToShortDateString();
                Dt.Rows.Add(dr);    //Add row in DataTable
            }

            //Assign Current Date, Month and Year

            strCurrntMonthYear = DateTime.Now.ToString("dd") + " " + DateTime.Now.ToString("MMMM") + " " + Year;

            //Assing DataTable to GridView
            gvCalander.DataSource = Dt;
            gvCalander.DataBind();

        }
    }

Try this it works..feel free to share your opinion to make it more relevant and advanced.

Comments

bygrace said…
Could you help me customize it so that I can enter the start date and end date and the grid view is dynamically generated?

Thank yOu
VedPathak said…
Hi bygrace,

Thanks for your feedback. Actully you can pass date from inut and split into Year and Month in code behind..on page load.

Let me know if you want more clarity on that.
Unknown said…
here '<%=strCurrntMonthYear %>' means what
VedPathak said…
Dear Karthika,

Thanks for your valuable feedback.

The code is actually not showing properly in html form, you please send me your email so can send your entire code.

actually strCurrntMonthYear is static variable declared in .cs page and used in .aspx page.

please have a look at below code,
Unknown said…
Hi please send me the entire code to my email azeem_alamgir@yahoo.com