首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从SQL到.Net monthCalendar.BoldDates?

从SQL到.Net monthCalendar.BoldDates?
EN

Stack Overflow用户
提问于 2012-04-21 23:53:13
回答 2查看 689关注 0票数 1

是否可以从SQL server中选择日期并将其用于monthCalendar中的BoldedDates?:)

在我使用以下代码的monthCalendar中使用BoldDates:

代码语言:javascript
复制
        DateTime[] Reserveret = new DateTime[3];
        Reserveret[0] = new DateTime(2012, 04, 25);
        Reserveret[1] = new DateTime(2012, 04, 01);
        Reserveret[2] = new DateTime(2012, 04, 12);

        monthCalendar1.BoldedDates = Reserveret;

这将在monthCalendar1中以粗体显示这3个日期。

但我不想硬编码这些日期,而是从我的SQL数据库中选择它们,该数据库包含3列,如下所示:

代码语言:javascript
复制
        ID       Room           Date
        1        103            2012-04-18
        2        106            2012-04-07
        3        103            2012-04-23
        4        103            2012-04-14
        5        101            2012-04-11

我的最终目标是按下forinstance Button103,然后在monthCalendar中使用粗体显示2012-04-18 & 2012-04-23 & 2012-04-14。但我不是数组方面的专家,我想这里会用到它:/

EN

回答 2

Stack Overflow用户

发布于 2012-04-22 00:09:44

试试像这样的东西。

代码语言:javascript
复制
protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
{ 
    for (int i = 0; i < Reserveret .Length; i++)
    {
        if (e.Day.Date == Reserveret ) 
        { 
            e.Cell.Font.Bold = true; 
            //e.Cell.BackColor = System.Drawing.Color.red;  //You may also try this 
        } 
     }
 } 
票数 0
EN

Stack Overflow用户

发布于 2014-01-28 15:52:00

我给这个问题加了+1,因为我正要问同样的问题,但我找到了这样做的方法,是的,他问的事情是可以做到的。

这篇关于Arrays and Collections的文章对我很有帮助,我在这个例子中使用了Generic List,因为我认为当我们从数据库中获取记录时,向动态集合中添加项目会更容易。我更改了一些代码以匹配问题。

代码语言:javascript
复制
//create a class and paste this.

public class createsCollection
{
    private static SqlConnection getConnection()
    {
        SqlConnection con = new SqlConnection();
        con.ConnectionString = @"Data Source=yourPCName\SQLEXPRESS;Initial Catalog=..";
        return con;
    }

    public static List <DateTime?> datesList(string room)
    {
      using (var con = new SqlConnection(getConnection().ConnectionString))
      {
        SqlDataReader dReader;
        List <DateTime?> dates = new List<DateTime?>();
        var cad = "SELECT Date from yourTbl where room = @Rn";

        using (var sqlCommand = new SqlCommand(cad, con))
        {
          try
          {   sqlCommand.CommandType = CommandType.Text
              sqlCommand.Parameters.AddWithValue("@Rn", room);
              con.Open();
              dReader = sqlCommand.ExecuteReader();

              if (dReader.HasRows == true)
              {
                while (dReader.Read())
                dates.Add(DateTime.Parse(dReader["Date"].ToString()));
              }
              return dates;
          }
          catch (Exception ex)
          { MessageBox.Show("Error: " + ex.Message);
            return null;
          }
        }
      }
    } 
  }

在您的表单中:

代码语言:javascript
复制
List<DateTime?> datesLst = new List<DateTime?>();
List<DateTime> notNullableList = new List<DateTime>(); 

private void Form_Load(object sender, EventArgs e)
{
  datesLst = createsCollection.datesList("103"); //this can be changed e.g Button.Text


  //We need to convert the nullable DateTime List 'datesLst' to a non-nullable 
  //DateTime array in order to pass it to the BoldedDates Property.

  if (datesLst != null) //if there were records from the db
  {
      foreach (DateTime? dtm in datesLst)
      {
         string str = dtm.GetValueOrDefault().ToShortDateString();
         DateTime val;

         if (DateTime.TryParse(str, out val))
         {
           notNullableList.Add(val); //add not-null value to the new generic list
         }
       }
       monthCalendar1.BoldedDates = notNullableList.ToArray(); //bold the dates gathered from the db to the Month Calendar
       monthCalendar1.Update();
       monthCalendar1.UpdateBoldedDates();
   }
}

这是一种方法,我相信有更好的方法来实现它,但当我测试它时,它对我来说工作得很好。

如果你想使用固定大小的Array而不是List,你可能想做一个根据指定条件返回(日期)行数的query,并将其设置为数组size,也不需要返回nullable列表,但我根据自己的喜好这样做了。干杯。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10260538

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档