在gridview中插入选定月份的日期 [英] insert day of selected month in gridview
本文介绍了在gridview中插入选定月份的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我如何在asp.net的gridview标签中插入选定月份的日期(通过下拉列表)
在asp.net表单中,有一个下拉列表,其中填充了月份(如jan,feb,3月等),而另一个是网格视图,当我们从下拉列表中选择任何月份时,该日期都填充在网格视图标签中. div class ="h2_lin">解决方案
检查此
int GetDaysInMonth( int 月,如果(月< 1 ||月> 12 ) { 抛出 新 System.ArgumentOutOfRangeException(" 月份",月份, 月份必须在1到12之间); } 如果( 1 == month || 3 ==月|| 5 ==月|| 7 ==月|| 8 ==月|| 10 ==月|| 12 ==个月) { 返回 31 ; } 其他 如果( 2 ==月) { // 检查leap年 如果( 0 ==(年% 4 )) { // 如果日期可被400整除,则为a年. // 否则,如果可以被100整除,则不能. 如果( 0 ==(年% 400 )) { 返回 29 ; } 其他 如果( 0 ==(年份% 100 )) { 返回 28 ; } // 可被4整除,但不能被100或400整除 // 这样就可以实现 返回 29 ; } // 不是a年 返回 28 ; } 返回 30 ; } 受保护的 无效 DropDownList1_SelectedIndexChanged(对象发件人,EventArgs e) { int days = GetDaysInMonth(Convert.ToInt32(DropDownList1.SelectedValue),Convert.ToInt32(DateTime.Now.Year)); DataTable myDT = 新 DataTable(); for ( int i = 0 ; i < 天; i ++) { DataColumn id = 新 DataColumn(" ); id.ColumnName = id + i.ToString(); id.DataType = System.Type.GetType(" ); myDT.Columns.Add(id); } for ( int i = 1 ; i < = 1 ; i ++) { DataRow dr = myDT.NewRow(); for ( int ii = 0 ; ii < 天; ii ++) { dr [ii] = ii.ToString(); } myDT.Rows.Add(dr); } GridView1.DataSource = myDT; GridView1.DataBind(); }
< div > ; < asp:DropDownList ID =" runat 服务器" AutoPostBack 真实" onselectedindexchanged =" DropDownList1_SelectedIndexChanged" < asp:ListItem 值 =" < > < asp:ListItem 值 =" < > < asp:ListItem 值 =" < > < asp:ListItem 值 =" < > < asp:ListItem 值 =" < > < asp:ListItem 值 =" < > < asp:ListItem 值 =" < > < asp:ListItem 值 =" < > < asp:ListItem 值 =" < > < asp:ListItem 值 =" < > < asp:ListItem 值 =" < > < asp:ListItem 值 =" < > < /asp:DropDownList > < br > < br > < asp:GridView ID =" runat 服务器" > < 列 > < /列 > < /asp:GridView > < /div >
最好的问候
米特瓦里(M.Mitwalli)
how i insert day of selected month(through dropdown list) in gridview lable in asp.net
in asp.net form there one drop-down list which filled with month like (jan,feb, march,etc) and another is grid view when we select any month from drop-down list the day filled in grid-view label.
解决方案
Hi ,
Check this
int GetDaysInMonth(int month, int year) { if (month < 1 || month > 12) { throw new System.ArgumentOutOfRangeException("month", month, "month mustbe between 1 and 12"); } if (1 == month || 3 == month || 5 == month || 7 == month || 8 == month || 10 == month || 12 == month) { return 31; } else if (2 == month) { // Check for leap year if (0 == (year % 4)) { // If date is divisible by 400, it's a leap year. // Otherwise, if it's divisible by 100 it's not. if (0 == (year % 400)) { return 29; } else if (0 == (year % 100)) { return 28; } // Divisible by 4 but not by 100 or 400 // so it leaps return 29; } // Not a leap year return 28; } return 30; } protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) { int days = GetDaysInMonth(Convert.ToInt32(DropDownList1.SelectedValue), Convert.ToInt32(DateTime.Now.Year)); DataTable myDT = new DataTable(); for (int i =0; i < days; i++) { DataColumn id = new DataColumn("id"); id.ColumnName = id + i.ToString(); id.DataType = System.Type.GetType("System.Int32"); myDT.Columns.Add(id); } for (int i = 1; i <= 1; i++) { DataRow dr = myDT.NewRow(); for (int ii = 0; ii < days; ii++) { dr[ii] = ii.ToString(); } myDT.Rows.Add(dr); } GridView1.DataSource = myDT; GridView1.DataBind(); }
<div> <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" onselectedindexchanged="DropDownList1_SelectedIndexChanged"> <asp:ListItem Value="1">jan</asp:ListItem> <asp:ListItem Value="2">feb</asp:ListItem> <asp:ListItem Value="3">mar</asp:ListItem> <asp:ListItem Value="4">apr</asp:ListItem> <asp:ListItem Value="5">may</asp:ListItem> <asp:ListItem Value="6">jun</asp:ListItem> <asp:ListItem Value="7">jul</asp:ListItem> <asp:ListItem Value="8">aug</asp:ListItem> <asp:ListItem Value="9">sep</asp:ListItem> <asp:ListItem Value="10">oct</asp:ListItem> <asp:ListItem Value="11">nov</asp:ListItem> <asp:ListItem Value="12">dec</asp:ListItem> </asp:DropDownList> <br /> <br /> <asp:GridView ID="GridView1" runat="server" > <Columns> </Columns> </asp:GridView> </div>
Best Regards
M.Mitwalli
这篇关于在gridview中插入选定月份的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文