Click here to Skip to main content
15,881,172 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi
I got a combbox where I select the month then I populates the start and end dates on textboxes, but now I want to dislay all dates from that month start date to the end date to be populated on a gridview column. for example 01,02,03 .....30 in a column

C#
protected void cmbOrganization_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
{
   txtFirstDay2.Text = GetFirstDayOfMonth(cmbOrganization.SelectedIndex +
           1).ToShortDateString();

   // get day of week for first day
   //string[] dateParts = txtFirstDay2.Text.Split('/');
   //DateTime dtFirstTemp = new
   //    DateTime(Convert.ToInt32(dateParts[2]),
   //    Convert.ToInt32(dateParts[0]),
   //    Convert.ToInt32(dateParts[1]));

   // display day of week in label
   //lblDowFirst2.Text = dtFirstTemp.DayOfWeek.ToString();

   // set last day
   txtLastDay2.Text = GetLastDayOfMonth(cmbOrganization.SelectedIndex +
       1).ToShortDateString();

   // get day of week for last day
   //string[] dateParts2 = txtLastDay2.Text.Split('/');
   //DateTime dtLastTemp = new
   //    DateTime(Convert.ToInt32(dateParts2[2]),
   //    Convert.ToInt32(dateParts2[0]),
   //    Convert.ToInt32(dateParts2[1]));

   // display day of week in label
   //lblDowList2.Text = dtLastTemp.DayOfWeek.ToString();

   //DateTime dt = new DateTime(dtpDate.Value.Year,
   //    dtpDate.Value.Month, 1);
}

#region Private Methods

/// <summary>
/// Get the first day of the month for
/// any full date submitted
/// </summary>
/// <param name="dtDate"></param>
/// <returns></returns>
private DateTime GetFirstDayOfMonth(DateTime dtDate)
{
   // set return value to the first day of the month
   // for any date passed in to the method

   // create a datetime variable set to the passed in date
   DateTime dtFrom = dtDate;

   // remove all of the days in the month
   // except the first day and set the
   // variable to hold that date
   dtFrom = dtFrom.AddDays(-(dtFrom.Day - 1));

   // return the first day of the month
   return dtFrom;
}

/// <summary>
/// Get the first day of the month for a
/// month passed by it's integer value
/// </summary>
/// <param name="iMonth"></param>
/// <returns></returns>
private DateTime GetFirstDayOfMonth(int iMonth)
{
   // set return value to the last day of the month
   // for any date passed in to the method

   // create a datetime variable set to the passed in date
   DateTime dtFrom = new DateTime(DateTime.Now.Year, iMonth, 1);

   // remove all of the days in the month
   // except the first day and set the
   // variable to hold that date
   dtFrom = dtFrom.AddDays(-(dtFrom.Day - 1));

   // return the first day of the month
   return dtFrom;
}

/// <summary>
/// Get the last day of the month for any
/// full date
/// </summary>
/// <param name="dtDate"></param>
/// <returns></returns>
private DateTime GetLastDayOfMonth(DateTime dtDate)
{
   // set return value to the last day of the month
   // for any date passed in to the method

   // create a datetime variable set to the passed in date
   DateTime dtTo = dtDate;

   // overshoot the date by a month
   dtTo = dtTo.AddMonths(1);

   // remove all of the days in the next month
   // to get bumped down to the last day of the
   // previous month
   dtTo = dtTo.AddDays(-(dtTo.Day));

   // return the last day of the month
   return dtTo;
}

/// <summary>
/// Get the last day of a month expressed by it's
/// integer value
/// </summary>
/// <param name="iMonth"></param>
/// <returns></returns>
private DateTime GetLastDayOfMonth(int iMonth)
{
   // set return value to the last day of the month
   // for any date passed in to the method

   // create a datetime variable set to the passed in date
   DateTime dtTo = new DateTime(DateTime.Now.Year, iMonth, 1);

   // overshoot the date by a month
   dtTo = dtTo.AddMonths(1);

   // remove all of the days in the next month
   // to get bumped down to the last day of the
   // previous month
   dtTo = dtTo.AddDays(-(dtTo.Day));

   // return the last day of the month
   return dtTo;
}


XML
<telerik:RadComboBox ID="cmbOrganization" Runat="server" AllowCustomText="True"
                         AutoPostBack="True"
                        onselectedindexchanged="cmbOrganization_SelectedIndexChanged">
    <Items>
        <telerik:RadComboBoxItem runat="server" Text="January"
            Value="January" />
        <telerik:RadComboBoxItem runat="server" Text="February"
            Value="February" />
        <telerik:RadComboBoxItem runat="server" Text="March"
            Value="March" />
    </Items>
</telerik:RadComboBox>
Posted
Updated 6-Jul-12 4:50am
v3
Comments
Shemeer NS 6-Jul-12 8:00am    
you wanted to populate all day (1,2,...30,) in a combobox inside a gridview column?
kolisa 6-Jul-12 8:07am    
not in a combbox ,in a gridview textbox column(default column)
_Zorro_ 6-Jul-12 11:44am    
Just an advice: You should post less code and be more explicit on what you're looking for.
[no name] 6-Jul-12 12:24pm    
"I want to dislay all dates"... so go ahead nothing stopping you from doing just that.

1 solution

There are several ways to do this.

Here are the most common approaches!

1) Bind the control to a list of dates
2) Use a MultiValueConverter and pass the start and end date in and return a string of dates.
 
Share this answer
 

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