Riley asked this 2 years ago

C# Error Constraint cannot be enabled as not all values have corresponding parent values

I have a C# .Net winforms application with two  DatagridViews that are linked as parent and child. When the user selects a record on the first grid then all the child records that are related to the selected record will be displayed on the second grid.

To achieve this, I have a dataset with two tables. and I have defined the parent and child data columns as below

DataColumn[] parentDC= new DataColumn[] {  data.Tables["Parent"].Columns["col1"], data.Tables["Parent"].Columns["col2"] };DataColumn[] childDC = new DataColumn[] { data.Tables["Child"].Columns["col1"], data.Tables["Child"].Columns["col2"] };

I have also defined a relationship as below.

DataRelation relation = new DataRelation("relation", parentDC, childDC); mydataSet.Relations.Add("SequenceDetails", parentDataCol, childDataCol, false);

The datagridviews are bound to the dataset as below

  parentDGV.DataSource = mydataSet;  parentDGV.DataMember = "Parent";  childDGV.DataSource = mydataSet;  childDGV.DataMember = "relation";

The following error occurs when i run the program,

"the constraint cannot be enabled as not all values have corresponding parent values"

any ideas what could be the reason?


Peter 2 years ago
DataRelation adds foreign key constraint on the tables. Check for every record in the parent table there is at least one record in the child table.