Click here to Skip to main content
15,895,142 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
this my code for load Database to datagrid
C#
var data = from p in dc.ToplantiTalepFormus from c in dc.Onays where 
               (p.Toplanti_Talep_ID == c.Toplanti_Talep_ID &&
               (c.Talep_eden_ID==ID || c.User_ID == ID))
           select new 
           {
               p.Toplanti_Talep_ID, p.Toplanti_Talep_Tarihi, 
               p.Toplanti_Tarihi, p.Toplanti_Saat, 
               p.Toplanti_Konusu, p.Toplanti_Yeri,
               p.Toplanti_Şekli, p.Toplantı_Durumu, 
               c.Toplantı_Onay, c.Yeni_Tarih, 
               c.Yeni_Saat, c.DeğişiklikOnay ,c.User_ID
           };

foreach (var ToplantiTalepFormus in data)
{
    Datagrid.ItemsSource = data.ToList();
}


What I have tried:

but when User Id is = user ID or somthing else I want to make row is red. Please help me
C#
private void Liste()
{
    ID = Convert.ToInt32(MainWindow.UserID);

    var data = from p in dc.ToplantiTalepFormus from c in dc.Onays where 
        p.Toplanti_Talep_ID == c.Toplanti_Talep_ID select new { p.Toplanti_Talep_ID,
        p.Toplanti_Talep_Tarihi, p.Toplanti_Tarihi, p.Toplanti_Saat,
        p.Toplanti_Konusu, p.Toplanti_Yeri, p.Toplanti_Şekli, p.Toplantı_Durumu,
        c.Toplantı_Onay, c.Yeni_Tarih, c.Yeni_Saat, c.DeğişiklikOnay, c.User_ID };

    foreach (var ToplantiTalepFormus in data)
    {
        if (ToplantiTalepFormus.User_ID == 1)
        {
            Datagrid.RowBackground = Brushes.Red;
        }
        Datagrid.ItemsSource = data.ToList();
    }
}
Posted
Updated 15-Sep-17 0:20am
v3
Comments
Graeme_Grant 15-Sep-17 5:14am    
or somthing else

We can't read your mind. Please update your question explaining how you want the condition to work.
ANIL AYDINALP 15-Sep-17 5:33am    
you can see my code when it is loading by my code from question if it is find user ID ==1 and then make that row make red I mean that
ANIL AYDINALP 15-Sep-17 5:51am    
[code moved to question]
this is I try to do it but When I do
if (ToplantiTalepFormus.User_ID == 1)
{
    Datagrid.RowBackground = Brushes.Red;
}

it gives all rows is red I need just specific row is red

1 solution

This wpf datagrid conditional row color - Google Search[^] found: wpf - How to set DataGrid's row Background, based on a property value using data bindings - Stack Overflow[^]

UPDATE: As you are not using data binding, here is a code-behind version.
C#
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        Mock();
    }

    public ObservableCollection<Person> Persons { get; }
        = new ObservableCollection<Person>();

    private void Mock()
    {
        var rnd = new Random();

        for (int i = 0; i < 100; i++)
        {
            Persons.Add(new Person
            {
                Name = $"Person {i}", 
                Age = rnd.Next(20, 50)
            });
        }

        DataGrid1.LoadingRow += DataGrid1_LoadingRow;
        DataGrid1.ItemsSource = Persons;
    }

    private void DataGrid1_LoadingRow(object sender, DataGridRowEventArgs e)
    {
        var row = e.Row;
        var person = row.DataContext as Person;
        if (person.Age > 30 && person.Age < 40)
        {
            row.Background = new SolidColorBrush(Colors.Red);
        }
    }
}

public class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
}


UPDATE #2: Here is a Hierarchical DataGrid version... Works the same, just wired up a little different...
C#
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        Mock();
    }

    public ObservableCollection<Parent> Parents { get; }
        = new ObservableCollection<Parent>();

    private void Mock()
    {
        var rnd = new Random();

        for (int i = 0; i < 100; i++)
        {
            var parent = new Parent { Name = $"Parent {i}", Age = rnd.Next(20, 50) };

            for (int j = 0; j < 20; j++)
            {
                parent.Children.Add(new Person
                {
                    Name = $"Child {i}",
                    Age = rnd.Next(1, 10)
                });
            }

            Parents.Add(parent);
        }

        DataGrid1.LoadingRow += DataGrid1_LoadingRow;
        DataGrid1.ItemsSource = Parents;
    }

    private void DataGrid1_LoadingRow(object sender, DataGridRowEventArgs e)
    {
        var row = e.Row;
        var person = row.DataContext as Person;
        if (sender == DataGrid1)
        {
            if (person.Age > 30 && person.Age < 40)
            {
                row.Background = new SolidColorBrush(Colors.Red);
            }
        }
        else if (person.Age > 4 && person.Age < 6)
        {
            row.Background = new SolidColorBrush(Colors.Green);
        }
    }
}

public class Parent : Person
{
    public ObservableCollection<Person> Children { get; }
        = new ObservableCollection<Person>();
}

public class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
}

And the XAML...
XML
<Window
    x:Class="DataGridCodeBehindRowColorTrigger.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    mc:Ignorable="d"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

    Title="CodeProject - DataGrid Custom Row Color"
    WindowStartupLocation="CenterScreen" Height="300" Width="600">
    <Grid>
        <DataGrid x:Name="DataGrid1"
                  GridLinesVisibility="None"
                  AlternatingRowBackground="GhostWhite" AlternationCount="1"
                  ScrollViewer.HorizontalScrollBarVisibility="Hidden"
                  AutoGenerateColumns="False" IsReadOnly="True"
                  RowDetailsVisibilityMode="VisibleWhenSelected"
                  VirtualizingPanel.ScrollUnit="Pixel">
            <DataGrid.Columns>
                <DataGridTextColumn Header="Name"
                                    Binding="{Binding Name}"
                                    Width="*" />
                <DataGridTextColumn Header="Age"
                                    Binding="{Binding Age}"
                                    Width="70"/>
            </DataGrid.Columns>
            <DataGrid.RowDetailsTemplate>
                <DataTemplate>
                    <DataGrid ItemsSource="{Binding Children}"
                              LoadingRow="DataGrid1_LoadingRow"
                              GridLinesVisibility="None"
                              AlternatingRowBackground="GhostWhite"
                              AlternationCount="1"
                              ScrollViewer.HorizontalScrollBarVisibility="Hidden"
                              AutoGenerateColumns="False" IsReadOnly="True">
                        <DataGrid.Columns>
                            <DataGridTextColumn Header="Name"
                                                Binding="{Binding Name}"
                                                Width="*" />
                            <DataGridTextColumn Header="Age"
                                                Binding="{Binding Age}"
                                                Width="70"/>
                        </DataGrid.Columns>
                    </DataGrid>
                </DataTemplate>
            </DataGrid.RowDetailsTemplate>
        </DataGrid>

    </Grid>

</Window>
 
Share this answer
 
v3
Comments
ANIL AYDINALP 15-Sep-17 6:50am    
it is good but how about two table show in one table and get specific row red on condation
Graeme_Grant 15-Sep-17 6:53am    
You never said stacked DataGrids. No different to the first. Data binding makes this much easier.
Graeme_Grant 15-Sep-17 7:55am    
Have a look at the update
ANIL AYDINALP 15-Sep-17 8:34am    
can I do with row index do you know it
Graeme_Grant 15-Sep-17 8:43am    
Not that I am aware of. All of the 3 solutions above work.

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