Click here to Skip to main content
15,885,740 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I have a UserControl in a WPF Desktop Application responsible for holidays. After selecting the date, the code is designed to color the cells that are displayed in the DataGrid, and are located between selected dates.

The code works fine for one row. If there are many rows, it colors all cells, and I would like to have only those that correspond to the dates in DataGrid.

My app (what view I have now):

http://oi59.tinypic.com/vipgyx.jpg[^][]

My code:
VB
Dim sql As String
        dgMyTeamView.DataContext = Nothing
        dgMyTeamView.Columns.Clear()
        dgMyTeamView.Visibility = Windows.Visibility.Visible
        curr = TryCast(dgMyTeamView.CurrentCell.Item, DataRowView)
        sql = "SELECT Enumber, HType, HStartDate, HEndDate from tbl_HolidayLog"
        Dim da As New SqlDataAdapter(sql, cnn)
        'Try
        Dim dt As New DataTable("Accs")
        da.Fill(dt)
        With Me.dgMyTeamView
            .ItemsSource = dt.DefaultView()
            .AutoGenerateColumns = True
            .Columns(0).Header = "Employee ID"
            .Columns(1).Header = "Vacation Type"
            .Columns(2).Header = "Start Date"
            .Columns(3).Header = "End Date"
        End With

        For Each col As DataGridColumn In dgMyTeamView.Columns
            Select Case col.Header.ToString
                Case "DateAdded"
                    If col.GetType.ToString = "System.Windows.Controls.DataGridTextColumn" Then
                        Dim dg As DataGridTextColumn = col
                        dg.Binding.StringFormat = "MM/dd/yyyy"
                    End If
            End Select
        Next
        Dim startDate As DateTime = dtStartV.SelectedDate
        Dim endDate As DateTime = dtEndV.SelectedDate
        Dim numberOfDays As Integer = ((endDate.Date) - (startDate.Date)).Days + 1
        'For Each hitem As DataRowView In dgMyTeamView.ItemsSource
        '    Dim hType As String = hitem.Item("HType")
        '    If hType = "Vacation" Then
        For dayCount As Integer = -1 To (numberOfDays - 0)
            Dim currentDate As Date = Convert.ToDateTime(startDate.AddDays(dayCount))
            Dim formatedD As String = currentDate.ToString("dd/MM")
            If currentDate.DayOfWeek <> DayOfWeek.Saturday AndAlso currentDate.DayOfWeek <> DayOfWeek.Sunday Then
                Dim c1 As New DataGridTextColumn
                Dim c3 As New DataGridTextColumn
                c1.Header = Format(currentDate, "MMMM") & vbCrLf & formatedD
                c3.Header = formatedD
                dgMyTeamView.Columns.Add(c1)
                For Each item As DataRowView In dgMyTeamView.ItemsSource
                    Dim saDate As DateTime = item.Item("HStartDate")
                    Dim enDate As DateTime = item.Item("HEndDate")
                    Dim numberOfHeader As Integer = ((enDate.Date) - (saDate.Date)).Days - 0
                    For dayCountHeader As Integer = 0 To (numberOfHeader - 0)
                        Dim currentDateHead As Date = Convert.ToDateTime(saDate.AddDays(dayCountHeader))
                        Dim formatedH As String = currentDateHead.ToString("dd/MM")
                        Dim c2 As New DataGridTextColumn
                        c2.Header = formatedH
                        If c2.Header = formatedD Then
                            Dim style As New Style(GetType(DataGridCell))
                            Dim setter As New Setter()
                            setter.Property = DataGridCell.BackgroundProperty
                            setter.Value = System.Windows.Media.Brushes.DarkBlue
                            style.Setters.Add(setter)
                            c1.CellStyle = style
                        End If
                    Next dayCountHeader
                Next
            End If
        Next dayCount


I would be very grateful for your help. Thank you in advance.
Posted

1 solution

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900