-1

我在数据库中的表有以下列 id , eventstart , eventend , eventname

我正在尝试将事件名称和事件开始日期绑定到日历单元格中的事件发生日期

但是我不能这样做

以下是我的代码片段

  protected void myCal_DayRender1(object sender, DayRenderEventArgs e)
{
    DataSet ds = new DataSet();

    SqlDataAdapter cmd = new SqlDataAdapter("Select * FROM event", con);
    cmd.Fill(ds, "Table");
    if (!e.Day.IsOtherMonth)
    {
        foreach (DataRow dr in ds.Tables[0].Rows)
        {
            if ((dr["eventstart"].ToString() != DBNull.Value.ToString()))
            {
                DateTime dtEvent = (DateTime)dr["eventstart"];
                if (dtEvent.Equals(e.Day.Date))
                {

                    Label lbl = new Label();
                    lbl.BorderColor = System.Drawing.Color.Black;
                    lbl.BorderStyle = BorderStyle.Double;
                    lbl.Width = 100;
                    lbl.Height = 100;
                    lbl.BackColor = System.Drawing.Color.BlanchedAlmond;
                    lbl.Text = TextBoxName.Text + "" + TextBoxStart.Text + "" + TextBoxEnd.Text;
                    e.Cell.Controls.Add(lbl);


                }
            }
        }
    }
    else
    {


      }

}

请帮助某人

4

1 回答 1

1

好的,从头再来一次。现在我想我明白了。您的问题可能是由于将日期与时间进行比较引起的。要仅获取没有时间的日期,您可以使用dt.Date如下所示。由于性能原因,您可以考虑在页面加载时加载所有数据。

DataSet ds = new DataSet();        

protected void Page_Load(object sender, EventArgs e)
{
    //get data
    using (SqlConnection connection = new SqlConnection(connectionString))
    {
        SqlDataAdapter adapter = new SqlDataAdapter();
        adapter.SelectCommand = 
            new SqlCommand("select * from event", connection);
        adapter.Fill(ds);
    }
}

protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
{        
    //mark dates in calendar
    foreach (DataRow dr in ds.Tables[0].Rows)
    {           
        DateTime dt = (DateTime)dr.Field<DateTime?>("eventstart");

        if (e.Day.Date == dt.Date)           
            {    
                e.Cell.BackColor = System.Drawing.Color.Yellow;

                //add event lable to day
                Label lbl = new Label();
                lbl.BorderColor = System.Drawing.Color.Black;
                lbl.BorderStyle = BorderStyle.Double;
                lbl.BackColor = System.Drawing.Color.BlanchedAlmond;
                lbl.Text = "Event text";
                e.Cell.Controls.Add(lbl);            
            }
    }
}
于 2012-09-20T16:51:59.970 回答