如何在DataGrid中只为一行着色? [英] How can I color only one row in DataGrid?

查看:118
本文介绍了如何在DataGrid中只为一行着色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我在WPF桌面应用程序中有一个UserControl负责假期。选择日期后,代码被设计为为DataGrid中显示的单元格着色,并且位于所选日期之间。



代码适用于一行。如果有很多行,它会为所有单元格着色,我希望只有那些与DataGrid中的日期对应的那些。



我的应用程序(我有什么视图)现在):



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



我的代码:

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:

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.

推荐答案

试试

在某些条件下更改DataGrid行的背景颜色 [ ^ ]

根据列值更改单个DataGridView行颜色 [ ^ ]

http://asimsajjad.blogspot.com/2010/10/changing-background-color-of-datagrid.html [ ^ ]
Try
Change Background Color of DataGrid Row on Some Condition [^]
Change individual DataGridView row colors based on column value[^]
http://asimsajjad.blogspot.com/2010/10/changing-background-color-of-datagrid.html[^]


这篇关于如何在DataGrid中只为一行着色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆