简体   繁体   中英

C# WPF DataGrid Change FontColour of Row Based on Value

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;
            }
        }
    }
}

}

  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>

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM