如何更改数据网格中的单元格颜色
How to change cell Color in datagrid
如何在检查单元格中的值时更改颜色?
例如:
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>
对值使用绑定 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));
}
}
}
}
如何在检查单元格中的值时更改颜色?
例如:
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>
对值使用绑定 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));
}
}
}
}