将GridView与日期绑定 [英] Binding a gridview with date

查看:86
本文介绍了将GridView与日期绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我有一个组合框,可以在其中选择月份,然后在文本框上填充开始日期和结束日期,但是现在,我希望将从该月开始日期到结束日期的所有日期都放置在gridview列中.例如一列中的01,02,03 ..... 30

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

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;
}





<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>

推荐答案

有几种方法可以做到这一点.

这是最常见的方法!

1)将控件绑定到日期列表
2)使用MultiValueConverter并传入开始和结束日期,并返回日期字符串.
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.


这篇关于将GridView与日期绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆