2012-04-21 6 views
1

SQLサーバーから日付を選択し、monthCalendarでBoldedDatesに使用できますか? :)私は次のコードを使用している月表示カレンダでBoldDatesへSQLから.NetへmonthCalendar.BoldDates?

 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日付。

しかし、その代わりに、私はこのような3列が含まれて私のSQLデータベースからそれらを選択したいと思いますこれらの日付ハードコーディング:

 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とforinstance押すことであろう2012-04- 18 & 2012-04-23 & 2012-04-14は、monthCalendarで太字になります。しかし、私はここで必要と思われる配列に関する専門家はいません。/

答えて

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 Appで作業しています。私はmonthCalendarでCell.Font.Boldを実行することはできません。 MonthCalendar1.BoldedDatesのようなものが必要です。 – user1348488

+0

参照用のリンクを参照してください:http://msdn.microsoft.com/en-us/library/f9a9k6dd.aspx –

0

はい、彼が求めているものを行うことが可能である、私は同じのために頼むことを約あったので、私はこの質問に+1を与えたが、私はそれを行う方法を発見しました。

この記事ではについてお答えしましたが、この例では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(); 
    } 
} 

それはそれを行うための方法だ、と私はそれを実装するための良い方法があると考えているが、私はそれをテストしたとき、これは私のためにかなり良い作品。

あなたの代わりにListの固定サイズArrayを使用したい場合は、あなたもあり、指定された条件に基づいて(日)の行数を返し、配列sizeにそれを設定された第1 queryを行うことをお勧めします返信する必要はありませんnullableリストしかし、私は自分の好みとしてそれをしました。乾杯。