Click here to Skip to main content
15,890,043 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to change the colour of a row if the date column is less than today or expired. It should show as red.

What I have tried:

I have tried using just XAML but this just sets every row to red.

My Converter Code

C#
public class CellDateColorConverter : IMultiValueConverter
    {

        public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (values[0] is DateTime && values[1] is DateTime)
            {

                DateTime Jobdate = (DateTime)values[0];
                DateTime DateToday = (DateTime)values[1];

                if (Jobdate.Date < DateToday.Date)
                {
                    return Color.Red;
                }
            }

            return System.Windows.Data.Binding.DoNothing;
    }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotSupportedException("CellDateColorConverter is a OneWay converter.");
        }
    }


XAML Code

XML
<DataGrid xmlns:System="clr-namespace:System;assembly=mscorlib"  Cursor="Hand" BorderBrush="#fbfbfb" BorderThickness="1" SelectionUnit="FullRow" IsTextSearchEnabled="True" IsTextSearchCaseSensitive="False" IsReadOnly="True" ScrollViewer.HorizontalScrollBarVisibility="Auto" ScrollViewer.VerticalScrollBarVisibility="Auto" Width="693" Height="520" x:Name="tblunallocatedjobs" ItemsSource="{Binding Path=tblunallocatedjobs}" AutoGenerateColumns="False" Margin="0,20,0,0" MouseDoubleClick="tblunallocatedjobs_MouseDoubleClick" Background="#fbfbfb">
                            <DataGrid.Resources>
                                <local:CellDateColorConverter x:Key="CellDateColorConverter"/>
                            </DataGrid.Resources>
                            <DataGrid.RowStyle>
                                <Style TargetType="{x:Type DataGridRow}">
                                    <Setter Property="Height" Value="40"></Setter>
                                    <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
                                    <Style.Triggers>
                                        <DataTrigger Binding="{Binding JobDate.Date}" Value="{x:Static System:DateTime.Today}">
                                            <Setter Property="Background" Value="#00ce3f"/>
                                        </DataTrigger>
                                    </Style.Triggers>
                                </Style>
                            </DataGrid.RowStyle>
                            <DataGrid.Columns>
                                <DataGridTextColumn Header="Job Number" Binding="{Binding ID}"/>
                                <DataGridTextColumn Header="Booked By" Binding="{Binding UserName}"/>
                                <DataGridTextColumn Header="Job" Binding="{Binding JobType}"/>
                                <DataGridTextColumn Header="Date" Binding="{Binding Path=JobDate, StringFormat=\{0:dd/MM/yyyy\}}" x:Name="JobDate">
                                    <DataGridTextColumn.ElementStyle>
                                        <Style TargetType="TextBlock">
                                            <Setter Property="Background">
                                                <Setter.Value>
                                                    <MultiBinding Converter="{StaticResource CellDateColorConverter}">
                                                        <Binding Path="JobDate"/>
                                                        <Binding Path="DateToday"/>
                                                    </MultiBinding>
                                                </Setter.Value>
                                            </Setter>
                                        </Style>
                                    </DataGridTextColumn.ElementStyle>
                                </DataGridTextColumn>
                                <DataGridTextColumn Header="Todays Date" Binding="{Binding Path=DateToday, StringFormat=\{0:dd/MM/yyyy\}}" x:Name="DateToday">
                                    <DataGridTextColumn.ElementStyle>
                                        <Style TargetType="TextBlock">
                                            <Setter Property="Background">
                                                <Setter.Value>
                                                    <MultiBinding Converter="{StaticResource CellDateColorConverter}">
                                                        <Binding Path="JobDate"/>
                                                        <Binding Path="DateToday"/>
                                                    </MultiBinding>
                                                </Setter.Value>
                                            </Setter>
                                        </Style>
                                    </DataGridTextColumn.ElementStyle>
                                </DataGridTextColumn>
                                <DataGridTextColumn Header="Start Time" Binding="{Binding JobTimeStart}"/>
                                <DataGridTextColumn Header="Finish Time" Binding="{Binding JobTimeFinish}" x:Name="JobTimeFinish"/>
                                <DataGridTextColumn Header="Post Code" Binding="{Binding PostCode}"/>
                                <DataGridTextColumn Header="Price" Binding="{Binding JobPrice}"/>
                                <DataGridTextColumn Header="Job Status" Binding="{Binding Allocation}"/>
                            </DataGrid.Columns>
                        </DataGrid>


Code Behind

C#
private void GetUnallocatedJobs()
       {
           using (SqlConnection conn = new SqlConnection(connectionString))
           {
               conn.Open();
               string query_search = "SELECT * FROM tblJobs WHERE (ID LIKE @ID OR PostCode LIKE @PostCode) AND Allocation='Unallocated' AND JobStatus != 'Cancelled' AND JobDate < @CurrentDate ORDER BY JobDate DESC, PostCode";
               SqlCommand cmd = new SqlCommand(query_search, conn);

               cmd.Parameters.AddWithValue("@CurrentDate", DateTime.Now);
               cmd.Parameters.AddWithValue("@ID", "%" + Search.Text + "%");
               cmd.Parameters.AddWithValue("@PostCode", "%" + Search.Text + "%");

               using (SqlDataAdapter adapter = new SqlDataAdapter(cmd))
               {
                   DataTable dt = new DataTable();

                   dt.Columns.Add("DateToday");
                   DataRow _ravi = dt.NewRow();
                   _ravi["DateToday"] = DateTime.Now.ToString("dd/MM/yyyy");
                   dt.Rows.Add(_ravi);



                   adapter.Fill(dt);

                   tblunallocatedjobs.ItemsSource = dt.DefaultView;
               }
               sqlCon.Close();


           }
       }
Posted
Updated 4-Dec-19 0:13am
v2

There are many ways.
* Before binding and at the time of generating model, on JobDate change decide color and bind to grid.
* Write LoadingRow event on datagrid
* Write own converter.
 
Share this answer
 
Comments
Maciej Los 3-Dec-19 4:27am    
5!BTW: I prefer to use value converters.
When MVVM pattern is in use, a value converter is very useful. See: WPF Tutorial | Value Converters[^]
 
Share this answer
 
Not sure exatly how you'd do it in WPF, but for Winforms it's pretty simple: Colouring DataGridView Cells According to their Content in WinForms[^] - I'd expect WPF would have similar events to hook into to do the same thing.
Doing it when it the cell is painted is the most efficient way, as no processing is done for cells that never get displayed (because the user doesn't scroll to them for example).
 
Share this answer
 
you could create boolean flag on your view model
public bool IsdateGrter => JobDate > Datetime.now; and map this pro to to trigger
<datatrigger binding="{Binding IsdateGrter }" value="true">
 
Share this answer
 

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