Hello all,
Working with ASP.NET, we wanted to print all the dates of months on the Header cell of GridView based on current month and year dynamically from code behind which can be changes later on by selecting different month and year from the two Dropdownlists provided on the UI.
To do that we have following code packet :
In our ASPX page we have :
<%@ Page Title="" Language="C#" AutoEventWireup="true"CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
<div style="border-top: 1px Dotted Grey; border-bottom: 1px Dotted Grey; padding: 7px; width: 60%; margin: 0 auto;">
<table width="100%">
<tr>
<td>Select Month
</td>
<td>
<asp:DropDownList ID="ddlMonth" runat="server" OnSelectedIndexChanged="ddlOnChange" AutoPostBack="true">
<asp:ListItem Value="1">January</asp:ListItem>
<asp:ListItem Value="2">February</asp:ListItem>
<asp:ListItem Value="3">March</asp:ListItem>
<asp:ListItem Value="4">April</asp:ListItem>
<asp:ListItem Value="5">May</asp:ListItem>
<asp:ListItem Value="6">June</asp:ListItem>
<asp:ListItem Value="7">July</asp:ListItem>
<asp:ListItem Value="8">August</asp:ListItem>
<asp:ListItem Value="9">September</asp:ListItem>
<asp:ListItem Value="10">October</asp:ListItem>
<asp:ListItem Value="11">November</asp:ListItem>
<asp:ListItem Value="12">December</asp:ListItem>
</asp:DropDownList>
</td>
<td>Department Name
</td>
<td>
<asp:DropDownList ID="ddlYear" runat="server" OnSelectedIndexChanged="ddlOnChange" AutoPostBack="true">
<asp:ListItem Value="2010">2010</asp:ListItem>
<asp:ListItem Value="2011">2011</asp:ListItem>
<asp:ListItem Value="2012">2012</asp:ListItem>
<asp:ListItem Value="2013">2013</asp:ListItem>
<asp:ListItem Value="2014">2014</asp:ListItem>
<asp:ListItem Value="2015">2015</asp:ListItem>
<asp:ListItem Value="2016">2016</asp:ListItem>
<asp:ListItem Value="2017">2017</asp:ListItem>
<asp:ListItem Value="2018">2018</asp:ListItem>
<asp:ListItem Value="2019">2019</asp:ListItem>
<asp:ListItem Value="2020">2020</asp:ListItem>
</asp:DropDownList>
</td>
</tr>
</table>
</div>
<asp:Panel ID="pnlros" runat="server" Visible="false">
<div style="width: 99.9%; overflow-x: scroll; margin-top: 5px;">
<asp:GridView ID="GridView" runat="server" BackColor="White">
<Columns>
<asp:TemplateField HeaderText="Sr_No.">
<ItemTemplate>
<center><asp:Label ID="lblsr" runat="server" Text='<%#Container.DataItemIndex + 1 %>'></asp:Label></center>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</asp:Panel>
</asp:Content>
In C# we Have:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Globalization;
public partial class _Default : System.Web.UI.Page
{
CultureInfo provider = CultureInfo.CreateSpecificCulture("en-US");
protected void Page_Load(object sender, EventArgs e)
{
binddropdown();
if (!IsPostBack)
{
string currentMonth = DateTime.Now.ToString("MMMM");
ddlMonth.SelectedIndex = ddlMonth.Items.IndexOf(ddlMonth.Items.FindByText(currentMonth));
string currentYear = DateTime.Now.ToString("YYYY");
ddlYear.SelectedIndex = ddlYear.Items.IndexOf(ddlYear.Items.FindByText(currentYear));
bindGrid();
}
}
protected void bindGrid()
{
DataTable dt = new DataTable();
int j = 0;
int year = Convert.ToInt32(ddlYear.SelectedValue.ToString());
int month = Convert.ToInt32(ddlMonth.SelectedValue.ToString());
DateTime current = new DateTime(year, month, 1);
string monthstr = current.ToString("MM", CultureInfo.InvariantCulture);
DateTime next = current.AddMonths(1);
int days = (next - current).Days;
for (int i = 1; i <= days; i++)
{
if (i < 10)
{
dt.Columns.Add("0" + i + "/" + monthstr + "/" + year);
}
else
{
dt.Columns.Add(i + "/" + monthstr + "/" + year);
}
}
GridView1.DataSource = dt;
GridView1.DataBind();
}
protected void ddlOnChange(object sender, EventArgs e)
{
bindGrid();
}
}
1 Comment(s)