Hi namojainashish,
Refer below code.
HTML
<form id="form1" runat="server">
<asp:ScriptManager runat="server" />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:BoundField DataField="Year" HeaderText="Year" />
<asp:TemplateField HeaderText="YearMonth">
<ItemTemplate>
<asp:UpdatePanel runat="server">
<ContentTemplate>
<asp:DropDownList runat="server" ID="ddly" AutoPostBack="true" OnSelectedIndexChanged="ddly_SelectedIndexChanged">
</asp:DropDownList>
<asp:DropDownList runat="server" ID="ddlm">
</asp:DropDownList>
</ContentTemplate>
</asp:UpdatePanel>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</form>
Namespaces
C#
using System.Data;
using System.Globalization;
VB.Net
Imports System.Data
Imports System.Globalization
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[] { new DataColumn("Id"), new DataColumn("Year") });
dt.Rows.Add(1, "2015");
dt.Rows.Add(2, "2017");
dt.Rows.Add(3, "2020");
dt.Rows.Add(4, "2021");
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
private void BindMonthandYearsInDropDown(DropDownList ddly, DropDownList ddlm, int year)
{
DateTimeFormatInfo info = DateTimeFormatInfo.GetInstance(null);
ddly.Items.Clear();
int CurrentYear = DateTime.Today.Year;
for (int i = 2015; i <= CurrentYear; i++)
{
ddly.Items.Add(i.ToString());
}
ddlm.Items.Clear();
ddly.SelectedIndex = ddly.Items.IndexOf(ddly.Items.FindByText(year.ToString()));
if (Convert.ToString(DateTime.Now.Year) == year.ToString())
{
for (int i = 1; i <= DateTime.Today.Month; i++)
{
ddlm.Items.Add(new System.Web.UI.WebControls.ListItem(info.GetMonthName(i), i.ToString()));
}
}
else
{
for (int i = 1; i < 13; i++)
{
ddlm.Items.Add(new System.Web.UI.WebControls.ListItem(info.GetMonthName(i), i.ToString()));
}
}
}
protected void ddly_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList ddly = sender as DropDownList;
GridViewRow row = ddly.NamingContainer as GridViewRow;
DropDownList ddlm = row.FindControl("ddlm") as DropDownList;
int year = Convert.ToInt32(ddly.SelectedValue);
BindMonthandYearsInDropDown(ddly, ddlm, year);
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList ddly = e.Row.FindControl("ddly") as DropDownList;
DropDownList ddlm = e.Row.FindControl("ddlm") as DropDownList;
int year = Convert.ToInt32(e.Row.Cells[0].Text.Trim());
BindMonthandYearsInDropDown(ddly, ddlm, year);
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn() {New DataColumn("Id"), New DataColumn("Year")})
dt.Rows.Add(1, "2015")
dt.Rows.Add(2, "2017")
dt.Rows.Add(3, "2020")
dt.Rows.Add(4, "2021")
GridView1.DataSource = dt
GridView1.DataBind()
End If
End Sub
Private Sub BindMonthandYearsInDropDown(ByVal ddly As DropDownList, ByVal ddlm As DropDownList, ByVal year As Integer)
Dim info As DateTimeFormatInfo = DateTimeFormatInfo.GetInstance(Nothing)
ddly.Items.Clear()
Dim CurrentYear As Integer = DateTime.Today.Year
For i As Integer = 2015 To CurrentYear
ddly.Items.Add(i.ToString())
Next
ddlm.Items.Clear()
ddly.SelectedIndex = ddly.Items.IndexOf(ddly.Items.FindByText(year.ToString()))
If Convert.ToString(DateTime.Now.Year) = year.ToString() Then
For i As Integer = 1 To DateTime.Today.Month
ddlm.Items.Add(New System.Web.UI.WebControls.ListItem(info.GetMonthName(i), i.ToString()))
Next
Else
For i As Integer = 1 To 13 - 1
ddlm.Items.Add(New System.Web.UI.WebControls.ListItem(info.GetMonthName(i), i.ToString()))
Next
End If
End Sub
Protected Sub ddly_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
Dim ddly As DropDownList = TryCast(sender, DropDownList)
Dim row As GridViewRow = TryCast(ddly.NamingContainer, GridViewRow)
Dim ddlm As DropDownList = TryCast(row.FindControl("ddlm"), DropDownList)
Dim year As Integer = Convert.ToInt32(ddly.SelectedValue)
BindMonthandYearsInDropDown(ddly, ddlm, year)
End Sub
Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
Dim ddly As DropDownList = TryCast(e.Row.FindControl("ddly"), DropDownList)
Dim ddlm As DropDownList = TryCast(e.Row.FindControl("ddlm"), DropDownList)
Dim year As Integer = Convert.ToInt32(e.Row.Cells(0).Text.Trim())
BindMonthandYearsInDropDown(ddly, ddlm, year)
End If
End Sub
Screenshot