Databinding and saving changes (if any) to new row

  • Thread starter Thread starter Michael Wong
  • Start date Start date
M

Michael Wong

Hi there!

I am new to databinding in .Net and I think I am going to get crazy...

What I need to do is this:
1) Create a new form for the user to enter a new Product
2) Save changes (if any) when the user closes the form

But I still couldn't get what I want, even after searching in Google.
Anyone can give me a pointer/hint?

Here is the details:

1) The form FormProduct (in which textboxes are bound) gets loaded:

private void FormProduct_Load(object sender, System.EventArgs e)
{
cmProduct = (CurrencyManager)BindingContext[dsProduct,"Products"];
LoadData();
cmProduct.AddNew();
}

2) Users either enter something or just close the form

3) Before closing the form, save any changes (I am stuck here):

private void FormProduct_Closing(object sender,
System.ComponentModel.CancelEventArgs e)
{
cmProduct.EndCurrentEdit(); // Saw this in several places already
if(dsProduct.HasChanges())
SaveData();
}

The problem is this:

if I don't call cmProduct.EndCurrentEdit(), then changes will not be
detected.
But if the user hasn't entered anything, they just close the form (like
a cancel), dsProduct.HasChanges() will return true (because we added a
row in the dataset).

Any solution?
 
Michael,

The easiest thing would be to have a Save and a Cancel button. If you only
want one button then you have to determine if they have entered a valid
entry. In either scenario, you wouldn't want them to enter invalid data so
you need to do validation.

If you are just entering one record data binding does not buy you a lot. Why
not just create a DataRow and add it to the table after the user input has
been validated?

David
 
Hi David,

I was thinking of doing it this way also, but then, I needed to have
more information on the form, using a tab, such as the suppliers, and
some other Product related records. So I'm thinking of using Databinding.

I may need to modify the UI.

Thanks a lot.
Michael,

The easiest thing would be to have a Save and a Cancel button. If you only
want one button then you have to determine if they have entered a valid
entry. In either scenario, you wouldn't want them to enter invalid data so
you need to do validation.

If you are just entering one record data binding does not buy you a lot. Why
not just create a DataRow and add it to the table after the user input has
been validated?

David



Hi there!

I am new to databinding in .Net and I think I am going to get crazy...

What I need to do is this:
1) Create a new form for the user to enter a new Product
2) Save changes (if any) when the user closes the form

But I still couldn't get what I want, even after searching in Google.
Anyone can give me a pointer/hint?

Here is the details:

1) The form FormProduct (in which textboxes are bound) gets loaded:

private void FormProduct_Load(object sender, System.EventArgs e)
{
cmProduct = (CurrencyManager)BindingContext[dsProduct,"Products"];
LoadData();
cmProduct.AddNew();
}

2) Users either enter something or just close the form

3) Before closing the form, save any changes (I am stuck here):

private void FormProduct_Closing(object sender,
System.ComponentModel.CancelEventArgs e)
{
cmProduct.EndCurrentEdit(); // Saw this in several places already
if(dsProduct.HasChanges())
SaveData();
}

The problem is this:

if I don't call cmProduct.EndCurrentEdit(), then changes will not be
detected.
But if the user hasn't entered anything, they just close the form (like a
cancel), dsProduct.HasChanges() will return true (because we added a row
in the dataset).

Any solution?
 
Instead of just automatically saving the changes, how about prompting the
user and asking if they want to save the changes.

~~Bonnie
 
Hi Bonnie,

Thanks for your advice, that's what I'm doing right now, and as David
suggested, I'm adding new row via a data row instead of using
databinding. It works very well (for now).
Instead of just automatically saving the changes, how about prompting the
user and asking if they want to save the changes.

~~Bonnie

:

Hi there!

I am new to databinding in .Net and I think I am going to get crazy...

What I need to do is this:
1) Create a new form for the user to enter a new Product
2) Save changes (if any) when the user closes the form

But I still couldn't get what I want, even after searching in Google.
Anyone can give me a pointer/hint?

Here is the details:

1) The form FormProduct (in which textboxes are bound) gets loaded:

private void FormProduct_Load(object sender, System.EventArgs e)
{
cmProduct = (CurrencyManager)BindingContext[dsProduct,"Products"];
LoadData();
cmProduct.AddNew();
}

2) Users either enter something or just close the form

3) Before closing the form, save any changes (I am stuck here):

private void FormProduct_Closing(object sender,
System.ComponentModel.CancelEventArgs e)
{
cmProduct.EndCurrentEdit(); // Saw this in several places already
if(dsProduct.HasChanges())
SaveData();
}

The problem is this:

if I don't call cmProduct.EndCurrentEdit(), then changes will not be
detected.
But if the user hasn't entered anything, they just close the form (like
a cancel), dsProduct.HasChanges() will return true (because we added a
row in the dataset).

Any solution?
 
Michael,

I don't know why you're not using DataBinding. It certainly makes life
easier in the long run. You should get used to doing it the right way now ...
it will make it easier if you learn how to handle this now ... and then when
you have more complex forms/controls to deal with in the future, you'll know
how to do it. =)

~~Bonnie


Michael Wong said:
Hi Bonnie,

Thanks for your advice, that's what I'm doing right now, and as David
suggested, I'm adding new row via a data row instead of using
databinding. It works very well (for now).
Instead of just automatically saving the changes, how about prompting the
user and asking if they want to save the changes.

~~Bonnie

:

Hi there!

I am new to databinding in .Net and I think I am going to get crazy...

What I need to do is this:
1) Create a new form for the user to enter a new Product
2) Save changes (if any) when the user closes the form

But I still couldn't get what I want, even after searching in Google.
Anyone can give me a pointer/hint?

Here is the details:

1) The form FormProduct (in which textboxes are bound) gets loaded:

private void FormProduct_Load(object sender, System.EventArgs e)
{
cmProduct = (CurrencyManager)BindingContext[dsProduct,"Products"];
LoadData();
cmProduct.AddNew();
}

2) Users either enter something or just close the form

3) Before closing the form, save any changes (I am stuck here):

private void FormProduct_Closing(object sender,
System.ComponentModel.CancelEventArgs e)
{
cmProduct.EndCurrentEdit(); // Saw this in several places already
if(dsProduct.HasChanges())
SaveData();
}

The problem is this:

if I don't call cmProduct.EndCurrentEdit(), then changes will not be
detected.
But if the user hasn't entered anything, they just close the form (like
a cancel), dsProduct.HasChanges() will return true (because we added a
row in the dataset).

Any solution?
 
For the moment, I'm only using databinding on part of the form's
controls because I don't really get what I want yet with a fully
databound form. Anyway, I'm still researching how to get it done.

Thank you
Michael,

I don't know why you're not using DataBinding. It certainly makes life
easier in the long run. You should get used to doing it the right way now ...
it will make it easier if you learn how to handle this now ... and then when
you have more complex forms/controls to deal with in the future, you'll know
how to do it. =)

~~Bonnie


:

Hi Bonnie,

Thanks for your advice, that's what I'm doing right now, and as David
suggested, I'm adding new row via a data row instead of using
databinding. It works very well (for now).
Instead of just automatically saving the changes, how about prompting the
user and asking if they want to save the changes.

~~Bonnie

:



Hi there!

I am new to databinding in .Net and I think I am going to get crazy...

What I need to do is this:
1) Create a new form for the user to enter a new Product
2) Save changes (if any) when the user closes the form

But I still couldn't get what I want, even after searching in Google.
Anyone can give me a pointer/hint?

Here is the details:

1) The form FormProduct (in which textboxes are bound) gets loaded:

private void FormProduct_Load(object sender, System.EventArgs e)
{
cmProduct = (CurrencyManager)BindingContext[dsProduct,"Products"];
LoadData();
cmProduct.AddNew();
}

2) Users either enter something or just close the form

3) Before closing the form, save any changes (I am stuck here):

private void FormProduct_Closing(object sender,
System.ComponentModel.CancelEventArgs e)
{
cmProduct.EndCurrentEdit(); // Saw this in several places already
if(dsProduct.HasChanges())
SaveData();
}

The problem is this:

if I don't call cmProduct.EndCurrentEdit(), then changes will not be
detected.
But if the user hasn't entered anything, they just close the form (like
a cancel), dsProduct.HasChanges() will return true (because we added a
row in the dataset).

Any solution?
 
I'm wondering what it is that you want to do that's not working for you with
databinding. If you get stuck in your research, don't hesitate to ask
questions. =)

~~Bonnie



Michael Wong said:
For the moment, I'm only using databinding on part of the form's
controls because I don't really get what I want yet with a fully
databound form. Anyway, I'm still researching how to get it done.

Thank you
Michael,

I don't know why you're not using DataBinding. It certainly makes life
easier in the long run. You should get used to doing it the right way now ...
it will make it easier if you learn how to handle this now ... and then when
you have more complex forms/controls to deal with in the future, you'll know
how to do it. =)

~~Bonnie


:

Hi Bonnie,

Thanks for your advice, that's what I'm doing right now, and as David
suggested, I'm adding new row via a data row instead of using
databinding. It works very well (for now).

Bonnie Berent [C# MVP] wrote:

Instead of just automatically saving the changes, how about prompting the
user and asking if they want to save the changes.

~~Bonnie

:



Hi there!

I am new to databinding in .Net and I think I am going to get crazy...

What I need to do is this:
1) Create a new form for the user to enter a new Product
2) Save changes (if any) when the user closes the form

But I still couldn't get what I want, even after searching in Google.
Anyone can give me a pointer/hint?

Here is the details:

1) The form FormProduct (in which textboxes are bound) gets loaded:

private void FormProduct_Load(object sender, System.EventArgs e)
{
cmProduct = (CurrencyManager)BindingContext[dsProduct,"Products"];
LoadData();
cmProduct.AddNew();
}

2) Users either enter something or just close the form

3) Before closing the form, save any changes (I am stuck here):

private void FormProduct_Closing(object sender,
System.ComponentModel.CancelEventArgs e)
{
cmProduct.EndCurrentEdit(); // Saw this in several places already
if(dsProduct.HasChanges())
SaveData();
}

The problem is this:

if I don't call cmProduct.EndCurrentEdit(), then changes will not be
detected.
But if the user hasn't entered anything, they just close the form (like
a cancel), dsProduct.HasChanges() will return true (because we added a
row in the dataset).

Any solution?
 
Ok then,

I'll try to explain myself clearly.
I would like to open a form to edit, or to add a product, depending on
which constructor it was called (much like what Outlook does)

To add a product, in the form's load event, I use
CurrencyManager.AddNew() as suggested in the newsgroup:

cmProduct = (CurrencyManager)BindingContext[dsProduct,"Products"];
LoadData();
cmProduct.AddNew();

Before closing the form, I check whether there is any modification, in
which case I prompt the user to confirm saving. And here is where I'm
stuck. I need to call cmProduct.EndCurrentEdit(), but this will only add
the new row to the dataset, which is not always true if the user haven't
entered anything (he just decided to close the form)

cmProduct.EndCurrentEdit(); // Saw this in the NG
if(dsProduct.HasChanges() && PrompSaving())
SaveData();

May be is there something I didn't understand yet?
I hope I haven't been too obscure in my explanation.
I'm wondering what it is that you want to do that's not working for you with
databinding. If you get stuck in your research, don't hesitate to ask
questions. =)

~~Bonnie



:

For the moment, I'm only using databinding on part of the form's
controls because I don't really get what I want yet with a fully
databound form. Anyway, I'm still researching how to get it done.

Thank you
Michael,

I don't know why you're not using DataBinding. It certainly makes life
easier in the long run. You should get used to doing it the right way now ...
it will make it easier if you learn how to handle this now ... and then when
you have more complex forms/controls to deal with in the future, you'll know
how to do it. =)

~~Bonnie


:



Hi Bonnie,

Thanks for your advice, that's what I'm doing right now, and as David
suggested, I'm adding new row via a data row instead of using
databinding. It works very well (for now).

Bonnie Berent [C# MVP] wrote:


Instead of just automatically saving the changes, how about prompting the
user and asking if they want to save the changes.

~~Bonnie

:




Hi there!

I am new to databinding in .Net and I think I am going to get crazy...

What I need to do is this:
1) Create a new form for the user to enter a new Product
2) Save changes (if any) when the user closes the form

But I still couldn't get what I want, even after searching in Google.
Anyone can give me a pointer/hint?

Here is the details:

1) The form FormProduct (in which textboxes are bound) gets loaded:

private void FormProduct_Load(object sender, System.EventArgs e)
{
cmProduct = (CurrencyManager)BindingContext[dsProduct,"Products"];
LoadData();
cmProduct.AddNew();
}

2) Users either enter something or just close the form

3) Before closing the form, save any changes (I am stuck here):

private void FormProduct_Closing(object sender,
System.ComponentModel.CancelEventArgs e)
{
cmProduct.EndCurrentEdit(); // Saw this in several places already
if(dsProduct.HasChanges())
SaveData();
}

The problem is this:

if I don't call cmProduct.EndCurrentEdit(), then changes will not be
detected.
But if the user hasn't entered anything, they just close the form (like
a cancel), dsProduct.HasChanges() will return true (because we added a
row in the dataset).

Any solution?
 
Michael,

When you call AddNew() you are adding the row. The row must be in the
dataset if you are binding to it. EndCurrentEdit() puts the edits into the
new row that you have added (you could also use the table's EndEdit()
method). A reason to call EndCurrentEdit() is that sometimes the user will
make changes and leave the focus in the control when they click the save
button (I think that will cause the last edit not to be saved, but there are
other scenarios). That last edit will not go into the table unless
EndCurrentEdit() is called.

I had thought that you only wanted to add a single row in this dialog.
Since you also want to edit also, data binding will do more for you. There
are many ways you could accomplish what you want. Perhaps, for consistency,
you could add the new row if you need to use the add functionality. Then
you can do your data binding just as if you were editing. However, if the
user cancels you need to delete the row that was added. You have to cancel
the edit if an existing row is being edited.

David



Michael Wong said:
Ok then,

I'll try to explain myself clearly.
I would like to open a form to edit, or to add a product, depending on
which constructor it was called (much like what Outlook does)

To add a product, in the form's load event, I use CurrencyManager.AddNew()
as suggested in the newsgroup:

cmProduct = (CurrencyManager)BindingContext[dsProduct,"Products"];
LoadData();
cmProduct.AddNew();

Before closing the form, I check whether there is any modification, in
which case I prompt the user to confirm saving. And here is where I'm
stuck. I need to call cmProduct.EndCurrentEdit(), but this will only add
the new row to the dataset, which is not always true if the user haven't
entered anything (he just decided to close the form)

cmProduct.EndCurrentEdit(); // Saw this in the NG
if(dsProduct.HasChanges() && PrompSaving())
SaveData();

May be is there something I didn't understand yet?
I hope I haven't been too obscure in my explanation.
I'm wondering what it is that you want to do that's not working for you
with databinding. If you get stuck in your research, don't hesitate to
ask questions. =) ~~Bonnie



:

For the moment, I'm only using databinding on part of the form's controls
because I don't really get what I want yet with a fully databound form.
Anyway, I'm still researching how to get it done.

Thank you

Bonnie Berent [C# MVP] wrote:

Michael,

I don't know why you're not using DataBinding. It certainly makes life
easier in the long run. You should get used to doing it the right way
now ... it will make it easier if you learn how to handle this now ...
and then when you have more complex forms/controls to deal with in the
future, you'll know how to do it. =)

~~Bonnie


:



Hi Bonnie,

Thanks for your advice, that's what I'm doing right now, and as David
suggested, I'm adding new row via a data row instead of using
databinding. It works very well (for now).

Bonnie Berent [C# MVP] wrote:


Instead of just automatically saving the changes, how about prompting
the user and asking if they want to save the changes.

~~Bonnie

:




Hi there!

I am new to databinding in .Net and I think I am going to get
crazy...

What I need to do is this:
1) Create a new form for the user to enter a new Product
2) Save changes (if any) when the user closes the form

But I still couldn't get what I want, even after searching in Google.
Anyone can give me a pointer/hint?

Here is the details:

1) The form FormProduct (in which textboxes are bound) gets loaded:

private void FormProduct_Load(object sender, System.EventArgs e)
{
cmProduct =
(CurrencyManager)BindingContext[dsProduct,"Products"];
LoadData();
cmProduct.AddNew();
}

2) Users either enter something or just close the form

3) Before closing the form, save any changes (I am stuck here):

private void FormProduct_Closing(object sender,
System.ComponentModel.CancelEventArgs e)
{
cmProduct.EndCurrentEdit(); // Saw this in several places
already
if(dsProduct.HasChanges())
SaveData();
}

The problem is this:

if I don't call cmProduct.EndCurrentEdit(), then changes will not be
detected.
But if the user hasn't entered anything, they just close the form
(like a cancel), dsProduct.HasChanges() will return true (because we
added a row in the dataset).

Any solution?
 
Hi Michael,

if you call cm.EndCurrentEdit(), the current changes from the bound controls
are collected into the underlying DataRow, and then the new row is made
permanent.

If there are no modified controls, you will consequently end up with an
blank (empty) new row. This is the way how databinding is designed to work.

Your problem is to determine, if the new row is empty or not, and then call
cm.CancelEdit() or cm.EndCurrentEdit(), respectively. If the row can be
modified through bound controls only (and not through code), you can check
the control.Modified flags. But I think, testing the individual row fields
is better, because the user might have filled some text in a control and
deleted it later.

This is a common problem in working with data, and not especially related to
databinding. If you look after "validation" or "user input validation", you
will find lots of material in the net. Tip: "declarative validation"...

HTH, ulrich.
 
Hi Ulrich,

I finally got it working.
With databinding, it's not possible for me to know if a new added row is
a blank one or not. So what I do is checking the controls on the form to
see whether they are different from the default values, such as:

txtDescription.text != "default description";
cboUnits.SelectedIndex != 1;
....

This may not be the best solution so any suggestion/help would be much
appreciated.

Thank you.


Ulrich said:
Hi Michael,

if you call cm.EndCurrentEdit(), the current changes from the bound controls
are collected into the underlying DataRow, and then the new row is made
permanent.

If there are no modified controls, you will consequently end up with an
blank (empty) new row. This is the way how databinding is designed to work.

Your problem is to determine, if the new row is empty or not, and then call
cm.CancelEdit() or cm.EndCurrentEdit(), respectively. If the row can be
modified through bound controls only (and not through code), you can check
the control.Modified flags. But I think, testing the individual row fields
is better, because the user might have filled some text in a control and
deleted it later.

This is a common problem in working with data, and not especially related to
databinding. If you look after "validation" or "user input validation", you
will find lots of material in the net. Tip: "declarative validation"...

HTH, ulrich.



Hi there!

I am new to databinding in .Net and I think I am going to get crazy...

What I need to do is this:
1) Create a new form for the user to enter a new Product
2) Save changes (if any) when the user closes the form

But I still couldn't get what I want, even after searching in Google.
Anyone can give me a pointer/hint?

Here is the details:

1) The form FormProduct (in which textboxes are bound) gets loaded:

private void FormProduct_Load(object sender, System.EventArgs e)
{
cmProduct = (CurrencyManager)BindingContext[dsProduct,"Products"];
LoadData();
cmProduct.AddNew();
}

2) Users either enter something or just close the form

3) Before closing the form, save any changes (I am stuck here):

private void FormProduct_Closing(object sender,
System.ComponentModel.CancelEventArgs e)
{
cmProduct.EndCurrentEdit(); // Saw this in several places already
if(dsProduct.HasChanges())
SaveData();
}

The problem is this:

if I don't call cmProduct.EndCurrentEdit(), then changes will not be
detected.
But if the user hasn't entered anything, they just close the form (like
a cancel), dsProduct.HasChanges() will return true (because we added a
row in the dataset).

Any solution?
 
Back
Top