Click here to Skip to main content
15,881,424 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
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..

XML
<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
C#
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
            }
        }
    }
}
Posted
Updated 10-Jun-11 20:35pm
v2

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[^]
 
Share this answer
 
v2
Comments
vishal_h 11-Jun-11 3:10am    
thanks but Sir my application is Window application not web..i already see this link and try some R&D but not helpful..please any thing else source..
[no name] 11-Jun-11 4:34am    
you need to process for search data on Datagrid1 either foreach or any other searching method with a Row object which u done on DataTable.
vishal_h 11-Jun-11 4:55am    
thank sir but what should i write here
private void button1_Click(object sender, RoutedEventArgs e)
{
DataRow[] result = dt.Select("PO >= 2");
// AND Sex = 'm'");
foreach (DataRow row in result)
{
MessageBox.Show(row[3].ToString());

}
}

Insted of message box i want to write here code to change the color and in message box i am getting the value of offline...
can you help me sir
[no name] 11-Jun-11 7:33am    
again you are still using datatable. Do search with Grid only.
Hope Change datagrid cell properties[^] article from CP might help you.
 
Share this answer
 
 
Share this answer
 

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