2012-04-21 22 views
1

是否可以从SQL服务器中选择日期并将它们用于monthCalendar中的BoldedDates? :)从SQL到.Net monthCalendar.BoldDates?

要BoldDates在我用下面的代码MONTHCALENDAR:

 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列像这样选择它们:

 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 

我的最终目标是按操作性的例子Button103然后2012-04- 2012-04-14 2012-04-23 &将在monthCalendar中Bolded。但我没有专家阵列,我认为这将需要在这里:/

回答

0

尝试一些这样的事情。

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

哎呀,不能看到这段代码如何工作。 我在Visual Studio中的C#Windows应用程序工作,我不认为它可能做一个monthCalendar Cell.Font.Bold。需要类似MonthCalendar1.BoldedDates ... – user1348488 2012-04-21 16:50:22

+0

对于ref请参阅链接:http://msdn.microsoft.com/en-us/library/f9a9k6dd.aspx – 2012-04-21 16:58:13

0

我给+1到这个问题,因为我要问的是相同的,但我发现做到这一点,是的,这是可以做到什么他问。

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

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

在您的形式:

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名单,但我做它作为我的偏好。干杯。