//creates object of datagridtextcolum class
DataGridTextColumn dataGridTextColumn = new DataGridTextColumn();
// sets header name
dataGridTextColumn.Header = "Dynamic Column";
//creates object of binding class , accepts name of property as parameter , whose value to be display
Binding binding = new Binding("PropertyName");
// sets binding mode
binding.Mode = BindingMode.TwoWay;
// sets how trigger to be fired.
binding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
// assigned the object of binding class to object of datagridtextcolumn class;
dataGridTextColumn.Binding = binding;
//code for add style to row
Style Rowstyle = new Style(); // creates object of style class
Rowstyle.TargetType = typeof(DataGridRow);//sets target type as DataGrid row
Setter setterBackground = new Setter(); // create objects of setter class
setterBackground.Property = DataGridRow.BackgroundProperty;
setterBackground.Value = Brushes.Black;
Rowstyle.Setters.Add(setterBackground);
Setter setterForeGround = new Setter();
setterForeGround.Property = DataGridRow.ForegroundProperty;
setterForeGround.Value = Brushes.White;
Rowstyle.Setters.Add(setterForeGround);
dgvRate.RowStyle = Rowstyle;
Style DataGridCellStyle = new Style();
DataGridCellStyle.TargetType = typeof(DataGridCell);
DataTrigger DT = new DataTrigger();
Binding DataTriggerBinding = new Binding("bindingName");
DataTriggerBinding.Mode = BindingMode.Default;
DataTriggerBinding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
DT.Binding = DataTriggerBinding;
DT.Value = "<=100";
Setter DataTriggerSetter = new Setter();
DataTriggerSetter.Property = DataGridCell.BackgroundProperty;
DataTriggerSetter.Value = Brushes.Red;
DT.Setters.Add(DataTriggerSetter);
DataGridCellStyle.Triggers.Add(DT);
DataTrigger DT1 = new DataTrigger();
Binding DataTriggerBinding1 = new Binding("bindingName");
DataTriggerBinding1.Mode = BindingMode.Default;
DataTriggerBinding1.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
DT1.Binding = DataTriggerBinding;
DT1.Value = "<=200";
Setter DataTriggerSetter1 = new Setter();
DataTriggerSetter1.Property = DataGridCell.BackgroundProperty;
DataTriggerSetter1.Value = Brushes.Black;
DT1.Setters.Add(DataTriggerSetter1);
DataGridCellStyle.Triggers.Add(DT1);
DataTrigger DT2 = new DataTrigger();
Binding DataTriggerBinding2 = new Binding("bindingName");
DataTriggerBinding2.Mode = BindingMode.Default;
DataTriggerBinding2.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
DT2.Binding = DataTriggerBinding2;
DT2.Value = ">=200";
Setter DataTriggerSetter2 = new Setter();
DataTriggerSetter2.Property = DataGridCell.BackgroundProperty;
DataTriggerSetter2.Value = Brushes.Red;
DT2.Setters.Add(DataTriggerSetter2);
DataGridCellStyle.Triggers.Add(DT2);
dataGridTextColumn.CellStyle = DataGridCellStyle;
// added created column into column list of datagrid (dgvRates);
dgvRate.Columns.Add(dataGridTextColumn);