Home > Mobile >  How to change cell Color in datagrid
How to change cell Color in datagrid

Time:12-01

How can I change the color when I check the value in the cell?

For example:

If (0,85 < test1)
    DataGridView1.Cell...Color.Red
else If
    DataGridView1.Cell...Color.green

I have a project and I have 1000 double values and I need to check all of them ...

This is my code

using System;
using System.Collections.Generic;
using System.Windows;

namespace datagrid
{
    /// <summary>
    /// Interaktionslogik für MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public List<Double> ValueList1 = new List<Double>();
        
        public MainWindow()
        {
            InitializeComponent();
            ValueList1.Add(0.32);
            ValueList1.Add(0.90);
            ValueList1.Add(0.23);
            ValueList1.Add(0.88);
            testvalues john = new testvalues();
            john.test1 = ValueList1[0].ToString("P0");
            john.test2 = ValueList1[1].ToString("P0");
            john.test3 = ValueList1[2].ToString("P0");
            john.test4 = ValueList1[3].ToString("P0");
            dataGridView1.Items.Add(john);
        }

        public class testvalues
        {
            public string test1 { get; set; }
            public string test2 { get; set; }
            public string test3 { get; set; }
            public string test4 { get; set; }
        }
    }
}

XAML

<Window x:Class="datagrid.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:datagrid"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <DataGrid  Height="243" Margin="142,77,213,99"   x:Name="dataGridView1" Background="Black" AutoGenerateColumns="False" >
            <DataGrid.Columns>
                <DataGridTextColumn Header=" Test1" Width="100" Binding="{Binding test1}" />
                <DataGridTextColumn Header=" Test2" Width="100" Binding="{Binding test2}" />
                <DataGridTextColumn Header=" Test3" Width="100" Binding="{Binding test3}" />
                <DataGridTextColumn Header=" Test4" Width="100" Binding="{Binding test4}" />
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</Window>

CodePudding user response:

Use binding ItemSource instead of Items for values

public List<Double> ValueList1 = new List<Double>();
        public MainWindow()
        {
            InitializeComponent();
            ValueList1.Add(0.32);
            ValueList1.Add(0.90);
            ValueList1.Add(0.23);
            ValueList1.Add(0.88);
            testvalues john = new testvalues();
            john.test1 = ValueList1[0].ToString("P0");
            john.test2 = ValueList1[1].ToString("P0");
            john.test3 = ValueList1[2].ToString("P0");
            john.test4 = ValueList1[3].ToString("P0");
            //dataGridView1.Items.Add(john);            
            List<testvalues> list =new List<testvalues>{ john};
            dataGridView1.ItemsSource= list;
           
        }

        public IEnumerable<DataGridRow> GetDataGridRows(DataGrid grid)
        {
            var itemsSource = grid.ItemsSource as List<testvalues>;
            if (null == itemsSource) yield return null;
            foreach (var item in itemsSource)
            {
                var row = grid.ItemContainerGenerator.ContainerFromItem(item) as DataGridRow;
                if (null != row) yield return row;
            }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            var rows = GetDataGridRows(dataGridView1);

            foreach (DataGridRow row in rows)
            {
                foreach (DataGridColumn column in dataGridView1.Columns)
                {
                    if (column.GetCellContent(row) is TextBlock)
                    {
                        TextBlock cellContent = column.GetCellContent(row) as TextBlock;
                        //Example condition
                        if ((decimal.Parse(cellContent.Text.TrimEnd(new char[] { '%', ' ' })) / 100M) <= 0.85M)
                        cellContent.Background = new System.Windows.Media.SolidColorBrush(System.Windows.Media.Color.FromArgb(120, 0, 255, 0));
                    }
                }
            }

        }

CodePudding user response:

 if (column.GetCellContent(row) is TextBlock)
                    {
                        TextBlock cellContent = column.GetCellContent(row) as TextBlock;
                        //Example condition
                        if (cellContent.Text != null)
                        { 
                           

                        }
                        else
                        {
                            if ((decimal.Parse(cellContent.Text.TrimEnd(new char[] { '%', ' ' })) / 100M) >= 0.85M)
                         
                            {
                                cellContent.Background = new SolidColorBrush(Color.FromArgb(120, 0, 255, 0));
                            }
                            else
                            {
                                cellContent.Background = new SolidColorBrush(Color.FromArgb(120, 255, 0, 0));
                            }

                        }

but this doesn't work...

  • Related