如何在数据网格中改变单元格的颜色 [英] How to chage color of cell in data grid

查看:68
本文介绍了如何在数据网格中改变单元格的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只想为该行将单元格数据的颜色从红色更改为绿色,请帮助我...我已经发布了类似的问题,但是我没有得到我需要的答案.我也在Google中进行搜索,但没有解决办法..
这是我的代码...
Xaml代码..

I want to change the color of cell data online from red to green only for that row please help me...I have already post similar question but i am not getting answer what i need..I also search in google but not getting solution..
Here is my code...
Xaml code..

<Window x:Class="WpfApplication3.Window1"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    Title="Window1" Height="299" Width="503" xmlns:my="http://schemas.microsoft.com/wpf/2008/toolkit">
    <Grid>
        <my:DataGrid AutoGenerateColumns="False" Margin="10,10,61,51" Name="dataGrid1">
            <my:DataGrid.Columns>
                <my:DataGridTemplateColumn Header="Icon" Width="50" IsReadOnly="True" >
                    <my:DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Image Source="{Binding Path=ImgPath}" Width="20" Height="20"/>
                        </DataTemplate>
                    </my:DataGridTemplateColumn.CellTemplate>
                </my:DataGridTemplateColumn>
                <my:DataGridTextColumn Header="Position"  Binding="{Binding Path=PO}"   />
                <my:DataGridTextColumn Header=" Name" Binding="{Binding Path=NA}"  />
                <my:DataGridTextColumn Header="Gender" Binding="{Binding Path=GE}"  />
                <my:DataGridTemplateColumn Header="Status" Width="50" IsReadOnly="True"  >
                    <my:DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Label Name="lbl" Content="{Binding Path=ST}" Foreground="Green" />
                        </DataTemplate>
                    </my:DataGridTemplateColumn.CellTemplate>
                </my:DataGridTemplateColumn>
                <my:DataGridTextColumn Header="Machine" Binding="{Binding Path=MA}"  />
            </my:DataGrid.Columns>
        </my:DataGrid>
    </Grid>
</Window>




xaml.cs




xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data;

namespace WpfApplication3
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        public DataTable dt;
        public Window1()
        {
            InitializeComponent();
            dt = new DataTable();
            dt.Columns.Add("PO");
            dt.Columns.Add("NA");
            dt.Columns.Add("GE");
            dt.Columns.Add("ST");
            dt.Columns.Add("MA");
            dt.Columns.Add("ImgPath", typeof(BitmapImage));

            DataRow dr = dt.NewRow();
            dr[0] = "1";
            dr[1] = "SAM";
            dr[2] = "Male";

            dr[3] = "Offline";
            dr[4] = "Comp123";
            dr[5] = new BitmapImage(new Uri("/WpfApplication3;component/Images/play.png", UriKind.RelativeOrAbsolute));
            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dr[0] = "2";
            dr[1] = "JOY";
            dr[2] = "Male";

            dr[3] = "Online";

            dr[4] = "Comp124";
            dr[5] = new BitmapImage(new Uri("/WpfApplication3;component/Images/play.png", UriKind.RelativeOrAbsolute));
            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dr[0] = "3";
            dr[1] = "ADITYA";
            dr[2] = "Male";
            dr[3] = "Online";
            dr[4] = "Comp124";
            dr[5] = new BitmapImage(new Uri("/WpfApplication3;component/Images/play.png", UriKind.RelativeOrAbsolute));
            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dr[0] = "4";
            dr[1] = "VISHAL";
            dr[2] = "Male";
            dr[3] = "Offline";
            dr[4] = "Comp124";
            dr[5] = new BitmapImage(new Uri("/WpfApplication3;component/Images/play.png", UriKind.RelativeOrAbsolute));
            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dr[0] = "5";
            dr[1] = "ANAY";
            dr[2] = "Male";
            dr[3] = "Online";
            dr[4] = "Comp124";
            dr[5] = new BitmapImage(new Uri("/WpfApplication3;component/Images/play.png", UriKind.RelativeOrAbsolute));
            dt.Rows.Add(dr);

            dataGrid1.ItemsSource = dt.DefaultView;
            dataGrid1.SelectedValuePath = "NA";

            DataRow[] result = dt.Select("PO >= 2");
            // AND Sex = 'm'");
            foreach (DataRow row in result)
            {

                //here i want to change the color of online from red to green please help me
            }
        }
    }
}

推荐答案

您不能直接基于数据行设置或更改颜色.为此,您需要从网格中找到特定的行,然后需要在网格的单元格上应用单元格格式.
我认为这会有所帮助 [
you can''t directly set or change color based on datarow. For this you need to find that particular row from grid and then you need to apply cell formatting on grid''s cell.
I think this will help[^]


希望 ^ ]文章可能会对您有所帮助.
Hope Change datagrid cell properties[^] article from CP might help you.


^ ]


这篇关于如何在数据网格中改变单元格的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆