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;
}
}
}
}
}
DataGridCellif you want to change color for the entire row? That is NOT what i suggested..... Please look at my answer again. - jsanalyticsItemsSourcebinding to? Where's yourDataContext? - jsanalytics<Style>in yourDataGrid, 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 theStyleelement as one item to theItemscollection. - Hopeless