Hi, I have a datagrid that is loaded with data first (data1), then I want to set rows to Red foreground color where columns C2 matches a given list of strings.
case:
1. Load with Data1
-> provide List1 and rows with C2 matching this list should have Red foreground color
-> provide List2 and only rows with C2 matching this list should have Red foreground color
2. Load with Data 2
-> repeat with the above 2 Lists for C2 values
In the xaml I hard-coded for row to be Red where C2 value is "A", how to set this to match a list at runtime?
<Window x:Class="WpfDataGrid.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"><Grid><Grid.RowDefinitions><RowDefinition Height="Auto"></RowDefinition><RowDefinition Height="Auto"></RowDefinition></Grid.RowDefinitions><Grid.ColumnDefinitions><ColumnDefinition Width="Auto"></ColumnDefinition></Grid.ColumnDefinitions><StackPanel Grid.Row="0" Orientation="Horizontal"><Button Name="btnData1" HorizontalAlignment="Left" Margin="2,2,2,2" Click="btnData1_Click">Data1</Button><Button Name="btnData2" HorizontalAlignment="Left" Margin="2,2,2,2" Click="btnData2_Click">Data2</Button><Button Name="btnList1" HorizontalAlignment="Left" Margin="2,2,2,2" Click="btnList1_Click">List1</Button><Button Name="btnList2" HorizontalAlignment="Left" Margin="2,2,2,2" Click="btnList2_Click">List2</Button></StackPanel><DataGrid Name="dg" Grid.Row="1"><DataGrid.RowStyle><Style TargetType="{x:Type DataGridRow}"><Style.Triggers><DataTrigger Binding="{Binding C2}" Value="A"><Setter Property="Foreground" Value="Red"/></DataTrigger></Style.Triggers></Style></DataGrid.RowStyle></DataGrid></Grid></Window>
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 WpfDataGrid { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void btnData1_Click(object sender, RoutedEventArgs e) { DataTable dt = new DataTable(); dt.Columns.Add(new DataColumn("C1",typeof(System.String))); dt.Columns.Add(new DataColumn("C2", typeof(System.String))); dt.Rows.Add("R1", "A"); dt.Rows.Add("R2", "B"); dt.Rows.Add("R3", "C"); dt.Rows.Add("R4", "D"); dt.Rows.Add("R5", "D"); dt.Rows.Add("R6", "E"); dg.ItemsSource = null; dg.ItemsSource = dt.DefaultView; } private void btnData2_Click(object sender, RoutedEventArgs e) { DataTable dt = new DataTable(); dt.Columns.Add(new DataColumn("C1", typeof(System.String))); dt.Columns.Add(new DataColumn("C2", typeof(System.String))); dt.Rows.Add("R1", "F"); dt.Rows.Add("R2", "G"); dt.Rows.Add("R3", "H"); dt.Rows.Add("R4", "A"); dt.Rows.Add("R5", "H"); dt.Rows.Add("R6", "C"); dg.ItemsSource = null; dg.ItemsSource = dt.DefaultView; } private void btnList1_Click(object sender, RoutedEventArgs e) { List<String> strlist = new List<string>(); strlist.Add("A"); strlist.Add("B"); } private void btnList2_Click(object sender, RoutedEventArgs e) { List<String> strlist = new List<string>(); strlist.Add("A"); strlist.Add("C"); } } }
Thanks,
-srinivas y.
sri