none
DataGrid and triggers. WPF

    Question

  • I wondering if anyone can help to solve my problem...

    On my Hotel program I am trying to do data grid where user can see book and check in information. I made data grid which contains room numbers and have columns which header is date time from today. Tried to do something like that : if room number 777 is occupied from 8/01/2011 to 8/05/2011 five cell was red bellow that date time columns. and if same room is book from 8/07/2011 to 8/08/2011 that cells was yellow.

    This is my trigger which checks if room is occupied or booked. When StatusInNumb value is 2 its means dat room is occupied and 1 means room is booked

     

    <UserControl.Resources>
     <Style x:Key="RoomNumberValidationCellStyle"
         TargetType="{x:Type DataGridCell}">
      <Style.Triggers>
       <DataTrigger Binding="{Binding Path=StatusInNumb}" Value="2">
        <Setter Property="Background" Value="#FFFFA7A7" />
       </DataTrigger>
       <DataTrigger Binding="{Binding Path=StatusInNumb}" Value="1">
        <Setter Property="Background" Value="#FFD8E538"/>
       </DataTrigger>
      </Style.Triggers>
     </Style>
    </UserControl.Resources>
    
    
    This method creates datagrid columns

     

     

    public void CreateDataGrid(int range)
     {
      dataGrid1.Columns.Clear();
      HProDataContext db = new HProDataContext();
      var _RoomNumber = (from d in db.SelectRooms select d.roomnumber).ToList();
      var _RoomType = (from d in db.SelectRooms select d.roomtype).ToList();
      var _RoomStatus = (from d in db.SelectRooms select d.status).ToList();
      var _te = (from d in db.rooms select d.forroomsinfo).ToList();
      for (int i = 0; i < _RoomNumber.Count; i++)
      {
       _RoomsInfoCollection.Add(new RoomsInfoData { RoomNumber = _RoomNumber[i], RoomType = _RoomType[i], RoomStatus = _RoomStatus[i], IsRoomMatched = "true", StatusInNumb = Convert.ToInt32(_te[i]) });
      }
      dataGrid1.Columns.Clear();
    
      dataGrid1.ItemsSource = RoomsInfoCollection;
    
      dataGrid1.Columns.Add(new DataGridTextColumn
      {
       Header = "Room Type",
       Binding = new Binding("RoomType"),
       IsReadOnly = true
      });
    
      dataGrid1.Columns.Add(new DataGridTextColumn
        {
         Header = "Room Number",
         Binding = new Binding("RoomNumber"),
         IsReadOnly = true
        });
      dataGrid1.Columns.Add(new DataGridTextColumn
      {
       Header = "Status",
       Binding = new Binding("RoomStatus"),
       Foreground = Brushes.Red,
       IsReadOnly = true
      });
    
      for (int i = 0; i < range; i++)
      {
       int daysInMonth = DateTime.DaysInMonth(DateTime.Now.Year, DateTime.Now.Month);
       int Month = DateTime.Now.Month;
       int day = DateTime.Now.Day + i;
       if (day >= daysInMonth)
       {
        day = 1 + i;
        Month = Month + 1;
       }
       dataGrid1.Columns.Add(new DataGridTextColumn { Header = "" + Month + "/" + day + "/" + DateTime.Now.Year.ToString() + "", IsReadOnly = true });
      }
     }
    
    

     

    And This method save booked date time array in List

     

    private void BookedDate()
     {
      HProDataContext db = new HProDataContext();
    
      var _checkInYear = (from d in db.bookings select d.checkinyear).ToList();
      var _checkInMonth = (from d in db.bookings select d.checkinmonth).ToList();
      var _checkInDay = (from d in db.bookings select d.checkinday).ToList();
    
      var _checkOutYear = (from d in db.bookings select d.checkoutyear).ToList();
      var _checkOutMonth = (from d in db.bookings select d.checkoutmonth).ToList();
      var _checkOutDay = (from d in db.bookings select d.checkoutday).ToList();
    
      for (int i = 0; i < _checkInDay.Count; i++)
      {
       DateTime checkIn = new DateTime(_checkInYear[i], _checkInMonth[i], _checkInDay[i]);
       DateTime checkOut = new DateTime(_checkOutYear[i], _checkOutMonth[i], _checkOutDay[i]); 
       TimeSpan span = checkOut - checkIn;
       int k = Convert.ToInt32(span.TotalDays);
    
       for (int day = 0; day <= k; day++)
       {
        rangeBook.Add(checkIn.AddDays(day));
       }
    
      }
     }
    
    

     

    is how i use trigger. The problem of this trigger is that when i call cannot transfer room number

     

    private void button2_Click(object sender, RoutedEventArgs e)
     {
      foreach (var col in this.dataGrid1.Columns)
      {
       for (int i = 0; i < rangeBook.Count; i++)
       {
        var headerText = (string)col.Header;
        if (headerText == rangeBook[i].ToShortDateString())
        {
         col.CellStyle = this.FindResource("RoomNumberValidationCellStyle") as Style;
        }
       }
      }
    
    

    So what is problem can seen in screen shot. The room 777 is booked from 8/1/2011 to 8/5/2011 and room 3434 is occupied from 8/1/2011 to 8/2/2011. Please i anyone can help me dont be lazy will be thankful all my life. it is very very very important for me

     

    http://imageshack.us/photo/my-images/155/capturetx.png/

     


    Monday, August 01, 2011 3:10 PM

Answers