Programmatically change certain row background in datagrid WPF C#
I am developing a software which requires the datagrid to show list of
products in datagrid. And the expired products must be highlighted by a
red color. Here's my xaml for the datagrid.
<DataGrid Height="479" AutoGenerateColumns="False" Grid.Column="2"
Grid.Row="2" Margin="12,55,24,148" FontSize="12" Name="dgProductList"
CanUserResizeRows="False" CanUserReorderColumns="False"
CanUserSortColumns="False" VerticalGridLinesBrush="#FFbab3b3"
IsReadOnly="True" />
And here is the code.
//Setup the Datagrid
public void SetupdgSalesListDataGrid()
{
var col1 = new DataGridTextColumn();
col1.Header = "Product Name";
col1.Binding = new Binding("[0]");
dgSalesProductList.Columns.Add(col1);
col1 = new DataGridTextColumn();
col1.Header = "Total Count";
col1.Binding = new Binding("[1]");
dgSalesProductList.Columns.Add(col1);
col1 = new DataGridTextColumn();
col1.Header = "Price Per Item";
col1.Binding = new Binding("[2]");
dgSalesProductList.Columns.Add(col1);
col1 = new DataGridTextColumn();
col1.Header = "Expiry Date";
col1.Binding = new Binding("[3]");
dgSalesProductList.Columns.Add(col1);
this.dgSalesProductList.ItemsSource = ProductList;
foreach(var Item in TableProduct)
{
string[] tempValues = new string[4];
tempValues[0] = Item.Name;
tempValues[1] = Item.TotalCount;
tempValues[2] = Item.PricePerItem;
tempValues[3] = Item.ExpiryDate;
ProductList.Add(tempValues);
}
this.dgSalesProductList.Items.Refresh();
}
I found a similar post on stackoverflow which used loadingrow event, but
the application crashed on windows 7. Link
No comments:
Post a Comment