Is this possbile? I have a record that has a cell value that when it has a value, the record should be non-editable. If the value is null or empty, then you should be able to edit 3 of the 4 cells. Currently I assign the fieldlayouts in the AssigningFieldLayoutToItem event. I should be able to do something here right?
void theDataGrid_AssigningFieldLayoutToItem(object sender, AssigningFieldLayoutToItemEventArgs e) { if (((DataRowView)(e.Item)).Row["parent_id"].ToString().Length == 0) { if (theDataGrid.FieldLayouts.Exists("ParentLayout")) { e.FieldLayout = theDataGrid.FieldLayouts["ParentLayout"]; } else { theDataGrid.FieldLayouts.Add(GetStructureFieldLayout("ParentLayout")); e.FieldLayout = theDataGrid.FieldLayouts["ParentLayout"];
//If this item has a product_id value, then the record should not be editable. //If the item does not have a product_id value, then AllowEdit on the //model_id, part_id, and quantity cells. } } else { if (theDataGrid.FieldLayouts.Exists("ChildLayout")) { e.FieldLayout = theDataGrid.FieldLayouts["ChildLayout"]; } else { theDataGrid.FieldLayouts.Add(GetStructureFieldLayout("ChildLayout")); e.FieldLayout = theDataGrid.FieldLayouts["ChildLayout"];
//If this item has a product_id value, then the record should not be editable. //If the item does not have a product_id value, then AllowEdit on the //model_id, part_id, and quantity cells. } } }
private FieldLayout GetStructureFieldLayout(string keyName) { FieldLayout fl = new FieldLayout();
fl.Key = keyName; if (keyName == "ChildLayout") fl.Settings.LabelLocation = LabelLocation.Hidden;
Field fld = new Field(); fld.Name = "model_id"; fld.Label = "Model ID"; fld.Settings.AllowEdit = false; fl.Fields.Add(fld);
fld = new Field(); fld.Name = "part_id"; fld.Label = "Part ID"; fld.Settings.AllowEdit = false; fl.Fields.Add(fld);
fld = new Field(); fld.Name = "serial_id"; fld.Label = "Serial ID"; fld.Settings.AllowEdit = false; fl.Fields.Add(fld);
fld = new Field(); fld.Name = "product_id"; fld.Label = "Product ID"; fld.Settings.AllowEdit = false; fl.Fields.Add(fld);
fld = new Field(); fld.Name = "quantity"; fld.Label = "Quantity"; fld.Settings.AllowEdit = false; fl.Fields.Add(fld);
//The following four fields are needed to show the hierarchy in the grid. fld = new Field(); fld.Name = "PRODUCT_STRUCTURE_NEST"; fl.Fields.Add(fld);
fld = new Field(); fld.Name = "product_structure_id"; fld.Visibility = Visibility.Hidden; fl.Fields.Add(fld);
fld = new Field(); fld.Name = "parent_id"; fld.Visibility = Visibility.Hidden; fl.Fields.Add(fld);
fld = new Field(); fld.Name = "sequence"; fld.Visibility = Visibility.Hidden; fl.Fields.Add(fld);
return fl; }
Hi Rod -
I would listen to the XamDataGrid.EditModeStarting event and set EditModeStartingEventArgs.Cancel to true if you don't want the Cell to be edited. To determine if you want to allow editing, you can look at the Cell.Value property off the event args (and potentially also the Cell.Record.Cells["otherField"].Value property if you want to take the value of other fields into account)
Joe
Thanks, that was very helpful!
It would be nice/more efficient if there was just an AllowEdit property on each Cell itself so I could handle all of my custom cell settings in InitializeRecord, without having to also handle EditModeStarting
Maybe to be included in the future?
That works. Thanks! I just made them all editable by default and handle it in the event like so...
{
}
So does this mean that I would make every field editable by default? Currently on cell click does the following...
theDataGrid.FieldSettings.CellClickAction = CellClickAction.SelectRecord;
So does this mean that when I double click a cell the EditModeStarting event is fired?
Rod