Re: Databinding and saving changes (if any) to new row by aualias
aualias
Mon May 23 08:22:13 CDT 2005
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" <nospam@email.here> wrote in message
news:%23PW5Wt3XFHA.3040@TK2MSFTNGP14.phx.gbl...
> 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.
>
> Bonnie Berent [C# MVP] wrote:
>> 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" wrote:
>>
>>
>>>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
>>>>
>>>>
>>>>"Michael Wong" wrote:
>>>>
>>>>
>>>>
>>>>>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
>>>>>>
>>>>>>"Michael Wong" wrote:
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>>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?
>>>>>>>
>>>>>
>