DataTable RowChanging event occurs in Fill with EnforceConstraints

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Msdn documentation states that setting DataSet EnforceConstraints to false
should prevent RowChanging event from firing when the table is filled but it
does fire. Is this a bug?

My example is a simple Windows Forms application created by drag and drop to
display the contents of a simple SQL Server table in a DataGridView. The form
code is as follows:

private TestDataSet.TestTableDataTable mfldTestTable;

public FormTest()
{
InitializeComponent();

mfldTestTable = mctlTestDataSet.TestTable;
mfldTestTable.RowChanging += new
DataRowChangeEventHandler(mfldTestTable_RowChanging);
}

private void FormTest_Load(object sender, EventArgs e)
{
mctlTestDataSet.EnforceConstraints = false;
this.mctlTestTableTableAdapter.Fill(this.mctlTestDataSet.TestTable);
mctlTestDataSet.EnforceConstraints = true;
}

private void mfldTestTable_RowChanging(object sender, DataRowChangeEventArgs
e)
{
// Do Stuff
}

Thanks for any clarification regarding this matter.
 
Hi David,

I don't know where you're getting your information from, but I certainly
can't find such a remark on the MSDN .Net SDK. Looking at
http://msdn2.microsoft.com/en-us/library/system.data.dataset.enforceconstraints.aspx, I
see that the property does what it logically would seem to do: It prevents
the enforcing of constraints while it is true. This does NOT mean that it
prevents any changes to any elements contained in it, NOR does it mean that
the "changed" events for those elements will not fire. What it DOES mean is
that if any change Constraint is violated during a change, a
ConstraintException will not be fired.

--
HTH,

Kevin Spencer
Microsoft MVP
Logostician
http://unclechutney.blogspot.com

There is a madness to my method.
 
Hi Kevin,

I guess I should have provided the references. In MSDN Library "How to: Turn
Off Constraints While Filling a Dataset"
http://msdn2.microsoft.com/en-us/library/s3bxwk8b(vs.80).aspx there is a note
after the opening paragraph that states "Validation events (for example,
ColumnChanging, RowChanging, and so on) will not be raised when constraints
are turned off." It would seem that this note is a "documentation bug"
because there is no mention in the documentation for EnforceConstraints.

Thanks for your reply. I guess I will just set an "IsTableFilled" flag to
skip validation during the table fill.

Daniel
 
Hi Daniel,

I agree with Kevin. Setting the EnforceConstraints property of a DataSet to
false only means that constraint rules are not followed when attempting any
update operation in the DataSet.

Even if the EnforceConstraints property is set to false, the ColumnChanging
or RowChanging will still be raised when any update operation is made.

If you don't want to raise the RowChanging/ColumnChanging event while
filling the DataTable, I suggest that you unsubscribe these events before
filling the DataTable and then subscribe these events again later.

The following is a sample.

// unsubscribe the RowChanging event
mfldTestTable.RowChanging -= new
DataRowChangeEventHandler(mfldTestTable_RowChanging);

// fill the data table
this.mctlTestTableTableAdapter.Fill(this.mctlTestDataSet.TestTable);

// subscribe the RowChanging event again
mfldTestTable.RowChanging += new
DataRowChangeEventHandler(mfldTestTable_RowChanging);

As for the MSDN document "How to: Turn
Off Constraints While Filling a Dataset"
http://msdn2.microsoft.com/en-us/library/s3bxwk8b(vs.80).aspx , it seems to
be incorrect. I will report this to our product team.

Thank you for pointing it out to us!

Hope this helps.
If you have anything unclear, please feel free to let me know.


Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
Hi Daniel,

The araticle you mentioned does seem at best misleading. It may be that when
you call the Fill method, the events are fired regardless. I just haven't
ever tried to find out. I use a similar technique to yours, by setting a
_Filling boolean while the table is filling.

--
HTH,

Kevin Spencer
Microsoft MVP
Logostician
http://unclechutney.blogspot.com

There is a madness to my method.
 
Back
Top