1
votes

I'm retrieving data from a Database and placing the contents into a WPF DataGrid like so;

namespace ContractsExcel { public partial class UserSelection : Page { public UserSelection() { InitializeComponent(); dataGrid.CanUserAddRows = false; string username = Environment.UserName; userImage.Source = new BitmapImage(new Uri(@"C:\Users\DanD\Desktop\" + username+".jpg")); }

    private void FillDataGrid(object sender, RoutedEventArgs e)
    {
        string constr = ConfigurationManager.ConnectionStrings["dbfString"].ConnectionString;
        using (OleDbConnection dbfCon = new OleDbConnection(constr))
        {
            try
            {
                dbfCon.Open();
                DataTable dTable = new DataTable();
                string dbfQuery = "SELECT em_pplid, em_name, em_netname FROM employs WHERE em_netname NOT LIKE ''";
                OleDbCommand MyQuery = new OleDbCommand(dbfQuery, dbfCon);
                OleDbDataAdapter DA = new OleDbDataAdapter(MyQuery);
                DA.Fill(dTable);
                dataGrid.ItemsSource = dTable.AsDataView();
            }
            catch (OleDbException)
            {
                throw;
            }
        }
    }
}

}

What I would like to do is change the font colour of an entire row based on the contents of a row cell, for example if the order is cherished change the font colour of the row to red.

Whilst there are multiple questions about this topic existing already I couldn't find anything specific to this. Would this need to be done through XAML or C# and how woild I go about implementing this functionality?

Updated code with XAML;

    <DataGrid x:Name="dataGrid" Margin="0,0,10,0" Grid.ColumnSpan="3" ColumnWidth="*" FontSize="18.667">
        <DataGrid.RowStyle>
            <Style TargetType="DataGridRow">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding em_netname}" Value='Chris'>
                        <Setter Property="Foreground" Value="Red"></Setter>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </DataGrid.RowStyle>
    </DataGrid>

Code to populate DataContext in C#;

namespace ContractsExcel { public partial class UserSelection : Page { public UserSelection() { InitializeComponent(); dataGrid.CanUserAddRows = false; string username = Environment.UserName; userImage.Source = new BitmapImage(new Uri(@"C:\Users\DanD\Desktop\" + username+".jpg")); }

    private void FillDataGrid(object sender, RoutedEventArgs e)
    {
        string constr = ConfigurationManager.ConnectionStrings["dbfString"].ConnectionString;
        using (OleDbConnection dbfCon = new OleDbConnection(constr))
        {
            try
            {
                dbfCon.Open();
                DataTable dTable = new DataTable();
                string dbfQuery = "SELECT em_pplid, em_name, em_netname FROM employs WHERE em_netname NOT LIKE ''";
                OleDbCommand MyQuery = new OleDbCommand(dbfQuery, dbfCon);
                OleDbDataAdapter DA = new OleDbDataAdapter(MyQuery);
                DA.Fill(dTable);
                dataGrid.ItemsSource = dTable.AsDataView();
            }
            catch (OleDbException)
            {
                throw;
            }
        }
    }
}

}

2
Style targetted to another type must be present within <DataGrid.Resources></DataGrid.Resources>. - AnjumSKhan
Why are you changing style for DataGridCell if you want to change color for the entire row? That is NOT what i suggested..... Please look at my answer again. - jsanalytics
@jstreet Apologies I have updated it now. - CBreeze
Ok, great, does it work now? What is ItemsSource binding to? Where's your DataContext ? - jsanalytics
your XAML code is wrong, you cannot wrap <Style> in your DataGrid, it should be <DataGrid.RowStyle> or if you want some style for datagrid, it should be <DataGrid.Style>, otherwise it understands that you want to add the Style element as one item to the Items collection. - Hopeless

2 Answers

0
votes
  1. You need DataTriggers for such scenarios.
  2. Changing Foreground of a DataGridCell will change that of entire row as style is applied to all cells.

Below style will make cherished order rows appear to be with Red font color and rest rows will have font color as Aqua.

    <Style TargetType="DataGridCell">
    <Setter Property="Foreground" Value="Aqua"/>
    <Style.Triggers>
        <DataTrigger Binding="{Binding Order}" Value="Cherished">
            <Setter Property="Foreground" Value="Red"/>
        </DataTrigger>
    </Style.Triggers>
    </Style>

Another approach is to use

           <DataGrid.RowStyle>
                <Style TargetType="DataGridRow">
                    <Setter Property="Foreground" Value="Aqua"/>
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding Title}" Value="Pepsi">
                            <Setter Property="Foreground" Value="Red"/>
                        </DataTrigger>
                    </Style.Triggers>

                </Style>
            </DataGrid.RowStyle>
-1
votes

write data template for data grid with custom colour for rows. you are assigning data table directly to grid. so that you cannot change colour based on data. else you have to write model class for colour then only can set colour based on data.