Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/263.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将数据表绑定到日历?_C#_Asp.net_Calendar - Fatal编程技术网

C# 将数据表绑定到日历?

C# 将数据表绑定到日历?,c#,asp.net,calendar,C#,Asp.net,Calendar,我只是想问一下,如何将dataTable绑定到asp.net日历,我尝试了,但在页面加载时失败了,我创建了一个sude代码来扩展我想要的内容 private void populateCalendar(DataTable dt) { foreach (var row in dt.Rows) { //if dates are in dt chage background color to red if(Calenda

我只是想问一下,如何将dataTable绑定到asp.net日历,我尝试了,但在页面加载时失败了,我创建了一个sude代码来扩展我想要的内容

    private void populateCalendar(DataTable dt)
    {

        foreach (var row in dt.Rows)
        {    //if dates are in dt chage background color to red 
            if(Calendar1.date)
        }
    }

根据我对你问题的理解,我试图解决你的疑问

步骤1:在标记中拖放日历控件

  <asp:Calendar ID="Calendar1"  runat="server" ondayrender="Calendar1_DayRender"></asp:Calendar>

步骤2:在Calendar1\u DayRender上

   protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
    {
            DataTable dt = new DataTable();
            dt.Columns.Add("Date", typeof(DateTime));
            dt.Rows.Add(DateTime.Today);
            dt.Rows.Add(DateTime.Today.AddDays(10));
            dt.Rows.Add(DateTime.Today.AddDays(12));
            dt.Rows.Add(DateTime.Today.AddDays(8));
            dt.Rows.Add(DateTime.Today.AddDays(6));
            dt.Rows.Add(DateTime.Today.AddDays(9));
            dt.Rows.Add(DateTime.Today.AddDays(2));
            dt.Rows.Add(DateTime.Today.AddDays(1));
            dt.Rows.Add(DateTime.Today.AddDays(3));
            DateTime date = e.Day.Date;
            var query = from row in dt.AsEnumerable()
                    where row.Field<DateTime>("date") == date
                   select row;
            foreach (var d in query)
            {
                e.Cell.BackColor = System.Drawing.Color.Red;
            }

    }
受保护的无效日历1\u DayRender(对象发送方,DayRenderReventArgs e)
{
DataTable dt=新的DataTable();
添加(“日期”,类型(日期时间));
dt.Rows.Add(DateTime.Today);
dt.Rows.Add(DateTime.Today.AddDays(10));
dt.Rows.Add(DateTime.Today.AddDays(12));
dt.Rows.Add(DateTime.Today.AddDays(8));
dt.Rows.Add(DateTime.Today.AddDays(6));
dt.Rows.Add(DateTime.Today.AddDays(9));
dt.Rows.Add(DateTime.Today.AddDays(2));
dt.Rows.Add(DateTime.Today.AddDays(1));
dt.Rows.Add(DateTime.Today.AddDays(3));
日期时间日期=e.Day.date;
var query=来自dt.AsEnumerable()中的行
其中row.Field(“日期”)==日期
选择行;
foreach(查询中的变量d)
{
e、 Cell.BackColor=System.Drawing.Color.Red;
}
}

注意:我的示例中使用的数据表是日期的集合

我已经编辑了您的标题。请参见“”,其中共识是“不,他们不应该”。@TimSchmelter我在我的问题中添加了一些代码,这正是我想要的,但是如果您的dt有开始日期和结束日期,您如何更改日期范围背景色?您可以使用where row.Field(“日期”)>=开始日期和结束日期(“日期”)