【问题标题】:How to change cell Color in datagrid如何更改数据网格中的单元格颜色
【发布时间】:2022-01-06 12:30:27
【问题描述】:

检查单元格中的值时如何更改颜色?

例如:

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

我有一个项目,我有 1000 个双精度值,我需要检查所有这些值...

这是我的代码

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>

【问题讨论】:

    标签: c# xaml datagrid


    【解决方案1】:

    使用绑定 ItemSource 而不是 Items 作为值

    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));
                        }
                    }
                }
    
            }
    

    【讨论】:

    • 好的,谢谢你:) 这很棒....但是我怎样才能使 cellContent.text
    • if (cellContent.Text
    • 我的意思是我需要一个双倍的......
    • 好的,我不清楚条件,我没有包括那部分。我已经更新了答案,您可以使用 "if ((decimal.Parse(cellContent.Text.TrimEnd(new char[] { '%', ' ' })) / 100M)
    • 好的,谢谢,但是当我在第一行和第一列文本中有什么时...?
    【解决方案2】:
     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));
                                }
    
                            }
    

    但这不起作用...

    【讨论】:

    • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-17
    • 1970-01-01
    • 1970-01-01
    • 2011-12-23
    • 1970-01-01
    相关资源
    最近更新 更多