How are u doing today ?
Now coming to the point :)
According to MSDN
Changes to a DataTable are not final until you call the AcceptChanges method. When either AcceptChanges or RejectChanges is called on a row in the parent table, the AcceptRejectRule value determines whether or not changes are propagated to corresponding rows in the child table.
Here goes my code:
Expand|Select|Wrap|Line Numbers
- public partial class Form1 : Form
- {
- DataSet Childhood;
- DataTable parentTable;
- DataTable childTable;
- ForeignKeyConstraint FKC;
- public Form1()
- {
- InitializeComponent();
- }
- private void btnconnect_Click(object sender, EventArgs e)
- {
- parentTable = new DataTable("ParentTable");
- parentTable.Columns.Add("ParentID", typeof(int));
- parentTable.Columns["ParentID"].AutoIncrement = true;
- parentTable.Columns["ParentID"].AutoIncrementSeed = 1;
- parentTable.Columns.Add("ParentName", typeof(string));
- childTable = new DataTable("ChildTable");
- childTable.Columns.Add("ChildID", typeof(int));
- childTable.Columns["ChildID"].AutoIncrement = true;
- childTable.Columns["ChildID"].AutoIncrementSeed = 1;
- childTable.Columns.Add("ParentID", typeof(int));
- childTable.Columns.Add("ChildName", typeof(string));
- Childhood = new DataSet();
- Childhood.Tables.Add(parentTable);
- Childhood.Tables.Add(childTable);
- FKC = new ForeignKeyConstraint("ForeignKey", parentTable.Columns["ParentID"], childTable.Columns["ParentID"]);
- FKC.UpdateRule = Rule.Cascade;
- FKC.DeleteRule = Rule.Cascade;
- childTable.Constraints.Add(FKC);
- Childhood.EnforceConstraints = true;
- dataGridView1.DataSource = parentTable;
- dataGridView2.DataSource = childTable;
- }
- private void btnacceptchanges_Click(object sender, EventArgs e)
- {
- parentTable.AcceptChanges();
- }
- private void btncascade_Click(object sender, EventArgs e)
- {
- FKC.AcceptRejectRule = AcceptRejectRule.Cascade;
- }
- private void btnnone_Click(object sender, EventArgs e)
- {
- FKC.AcceptRejectRule = AcceptRejectRule.None;
- }
- private void btnrejectchanges_Click(object sender, EventArgs e)
- {
- parentTable.RejectChanges();
- }
- }
Also I create one parent(in parent table) and create 3 children(in child table) of it.
I click on btnnone to make AcceptRejectRule = AcceptRejectRule.None
now i change the parent ID of the parent and subsequently the child rows are affected(as per the ForeignKeyConstraint ).
Now since I had set AcceptRejectRule = AcceptRejectRule.None I expected that when i click btnacceptchanges the parent row and the child rows would revert back to their original value.
But sadly it does not happen.
The changes get saved !
Now where am i mistaken ???
Thanks !