Dataset.HasChanges() returns True when binding to UserControl prop

G

Guest

Here is the info:

..Net 1.1, C#, Windows Forms
I have a single windows form. I load in a dataset with some data. I bind
the dataset to a grid to a particular DataTable. All works as expected. I
create a user control, stick a text box on it. Expose the Text Property of
the text box as a public property on the UserControl, such as:
public string FirstName{get{return textBox1.Text;}set{textBox1.Text=value;}
I add the user control to the main form by calling this.controls.Add(contr).
I set the databinding by using
usercontrol.DataBindings.Add("Text",dataset,"Customer.FirstName");

So now the form has a datagrid on it, with a usercontrol, which has a
textbox. The form loads, the grid is populated, the CUrrencyManager position
is at 0, the text box is populated with the same data as the right grid cell.
Then you click on the different cells in the grid, but don't make any
changes. Then call dataSet.HasChanges() - > returns true. Why? To test the
theory, remove the line that does the databinding of the user control.
Selecting different rows, does not alter the rowstates on the datarows. I
don't know why this would be, and would really really appreciate help. This
is a case od databinding working too well. :). Thank you in advance, sorry
for the lengthy explanation.
 
B

Bart Mermuys

Hi,

Paul K said:
Here is the info:

.Net 1.1, C#, Windows Forms
I have a single windows form. I load in a dataset with some data. I bind
the dataset to a grid to a particular DataTable. All works as expected.
I
create a user control, stick a text box on it. Expose the Text Property
of
the text box as a public property on the UserControl, such as:
public string FirstName{get{return
textBox1.Text;}set{textBox1.Text=value;}
I add the user control to the main form by calling
this.controls.Add(contr).
I set the databinding by using
usercontrol.DataBindings.Add("Text",dataset,"Customer.FirstName");

So now the form has a datagrid on it, with a usercontrol, which has a
textbox. The form loads, the grid is populated, the CUrrencyManager
position
is at 0, the text box is populated with the same data as the right grid
cell.
Then you click on the different cells in the grid, but don't make any
changes. Then call dataSet.HasChanges() - > returns true. Why?

Most likely because there is no similar named Changed event, so the property
value always get's persisted at Control.Validating. And a DataRow doesn't
care if it's the same value, if a field is set then it's marked as changed.

Add a changed event to your UserControl, eg:

public event EventHandler FirstNameChanged;

public string FirstName
{
get{return textBox1.Text;}set{textBox1.Text=value;}
}

// attach an eventhandler to textBox1.TextChanged
private textBox1_OnTextChanged( object sender, EventArgs e )
{
if ( FirstNameChanged!=null )
FirstNameChanged(this, EventArgs.Empty);
}

With a Changed event, it will persist the value at Control.Validating but
only if the Changed event has fired before the Validating event.


HTH,
Greetings
 
G

Guest

Thank you for the fastest response one could hope for. On top of that,
everything worked like a charm. I knew it had to be something I was doing,
but didn't have a clue. I can't thank you enough for pointing this out.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top