2012-04-21 40 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名單,但我做它作爲我的偏好。乾杯。