Monday, September 5, 2016

Change XamDataGrid Cell based on values of other cells in code behind – WPF

Sometimes we have requirement to enable/disable single cell value in data grid based on the selections of other cells.

Scenario:
There were XamDataGrid which user able to add purchasing details for their warehouse. When user purchase materials from different units like (Meters, Kilograms) however user only use Yard in their manufacturing process.

If user select Yard for their unit and they get those materials from Meters, then user need to set Meter to Yard conversion automatically and freeze the conversion cell.


How to implement:
Add value change event to the Unit columns


<EventSetter Event="igEditors:ValueEditor.ValueChanged" Handler="PUOM_OnValueChanged" />

<EventSetter Event="igEditors:ValueEditor.ValueChanged" Handler="SUOM_OnValueChanged" />
Code-behind:
Based on the conditions user can add any conditions to filter values. “Conversion” is the column name contains the cell.

private void SUOM_OnValueChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
{
   DataRecord dt = (DataRecord)xamGridLines.ActiveRecord;
   Cell cellConversion = dt.Cells["Conversion"] as Cell;
   var cellEditor = Infragistics.Windows.DataPresenter.CellValuePresenter.FromCell(cellConversion).Editor;
   cellEditor.IsReadOnly = true;
}
If user need to inactive entire column use following code snippet
private void SUOM_OnValueChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
{
    xamGridLines.FieldLayouts[0].Fields["Conversion"].Settings.AllowEdit = false;
    xamGridLines.ActiveRecord.FieldLayout.Fields["Conversion"].Settings.AllowEdit = false;
}