Click here to Skip to main content
15,868,109 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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.
Posted
Updated 5-Sep-12 19:39pm
v2
Comments
manognya kota 6-Sep-12 1:07am    
Could you explain more? With some code snippets what you have tried.
FIROZ KHANIT 6-Sep-12 1:41am    
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.
manognya kota 6-Sep-12 3:32am    
Correct me if iam wrong.
Are you replicating something like calendar in the gridview for that month?
FIROZ KHANIT 6-Sep-12 4:04am    
i want to fill the day in grid view label.. for exmp:- in march there are 31 day if i select march from drop down list then all day of march are filled in grid view label not in text box.
Kuthuparakkal 6-Sep-12 1:18am    
more info plz

1 solution

Hi ,
Check this
C#
    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();
   }


XML
<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
 
Share this answer
 
v4
Comments
FIROZ KHANIT 6-Sep-12 3:24am    
i want to fill all day of selected month in grid-view..
Mohamed Mitwalli 6-Sep-12 3:28am    
like 1 ,2 ,3 ... ,etc
FIROZ KHANIT 6-Sep-12 3:30am    
yes.
Mohamed Mitwalli 6-Sep-12 5:47am    
i already update solution check it
FIROZ KHANIT 6-Sep-12 3:33am    
in horizontal line

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900