c# - Detect if deleting DataGrid row is valid to delete -
i use ef code-first datagrid itemssourse, how can detect on previewkeyup event if items user want delete valid deleted or not.
for example, if user delete 'customer', , 'order' contains customerid illegal. there way know if identifier of item user want delete being used foreign key in table? ef has information on it?
something that:
private void datagridex_previewkeyup(object sender, system.windows.input.keyeventargs e) { if (e.key == key.delete) { if (e.originalsource datagridcell) { datagrid datagrid = sender; if (!isvalidtodelete(datagrid.selectedcells)) { e.handled = true; msgbox("not valid delete !"); } } } }
it sounds there's selected row in customer
grid, , you're evaluating whether or not delete selected customer
.
and customer
, order
both in ef model.
so customer
, find id, , write query checks order
table in ef model orders customer id. here's idea, semantically -- actual code bit different, no doubt.
var selcustomerid = (datagrid.selectedrow customer).customerid; var customerhasorder = myefmodel.order.any(ordr => ordr.customerid == selcustomerid);
think of any
call being loop that'll call lambda each row in myefmodel.order
, , return true first time lambda returns true.
ordr => ordr.customerid == selcustomerid
in reality, of course, since you're doing linq ef, it's generating sql query.
Comments
Post a Comment