public virtual void CheckRule() { try { if (this.Parent is IMyContainer && this.oBinding != null) { DataTable table = (DataTable)this.oBinding.DataSource; string field = this.oBinding.BindingMemberInfo.BindingField; int row = this.BindingContext[table].Position; if (((IMyContainer)this.Parent).CheckRule(table, field, row) > 0) this.IsRed = true; else this.IsRed = false; } else this.IsRed = false; } catch (Exception) { this.IsRed = false; } } public virtual void CheckColors() { if (this.IsRed) { if (this.Enabled == true) { this.BackColor = System.Drawing.Color.Red; this.ForeColor = System.Drawing.Color.White; } else { this.BackColor = System.Drawing.SystemColors.Control; this.ForeColor = System.Drawing.Color.Red; } } else { if (this.Enabled == true) { this.BackColor = System.Drawing.SystemColors.Window; this.ForeColor = System.Drawing.SystemColors.WindowText; } else { this.BackColor = System.Drawing.SystemColors.Control; this.ForeColor = System.Drawing.SystemColors.WindowText; } } }The CheckRule() method in each Parent container basically passes the call up through each Parent (which can be things like Panel, GroupBox, Usercontrol, etc.) until it gets to the top (the Form). The base class for the Form doesn't do anything, that's up to the individual sub-classed Forms. We have a pretty complicated Rules class which contains a RulesCollection that's basically populated from the database from the Form. But, here's an example of how a Form might handle the CheckRule method, using the oRules collection. The RulesCollection.CheckRule() method does all the work.:
public override int CheckRule(DataTable data, string field, int row) { return this.oRules.CheckRule(data, field, row); }You can also loop through every Rule comparing against the DataSet.
protected virtual int CheckRules(DataSet ds, MyRule rule) { int num = 0; if (rule.Table.DataSet.Equals(ds) && ds.HasChanges()) { for (int it = 0; it < ds.Tables.Count; it++) if (rule.Table.Equals(ds.Tables[it])) for (int ir = 0; ir < ds.Tables[it].Rows.Count; ir++) if (ds.Tables[it].Rows[ir].RowState != DataRowState.Unchanged) num += this.CheckRule(ds.Tables[it], rule.Field, ir); } return num; }The above method would be called in your Form's Save method, like this:
if (this.oData.HasChanges()) { for (int i = 0; i < this.oRules.Count; i++) num += this.CheckRules(this.oData, this.oRules[i]); }Anyway, that's enough for now. Enough for you to get the picture I hope.