I have a .NET 2.0 Winforms app (and Subsonic 2.2), where I have a DataGridView and few controls in a panel, both are bound to a BindingSource whose datasource is a Subsonic ActiveRecord collection. When a user selects a row in grid, the corresponding details are shown in the panel below in appropriate controls (texbox, combo, datetimepicker, etc.)
When a user changes some data, and tries to move to some other row, I want the app to display a warning that data has changed and it needs to be saved. If user selects yes, the data should be persisted.
I face a few issues:
a) In the DGV.RowEnter event handler, I try to find if the current row is dirty (using IsDirty of activerecord). If so, I display a messagebox.
b) When I save and rebind the grid, the datasource of the datagridview cannot be re-bound to a new (saved) collection. The app throws error that 'operation is invalid'.
c) Also, since both the gridview and controls are bound to same collection, whenever I change something in the control, it is reflected in grid, but when user selects 'No' to save data, the changes should be undone. Does subonic has any Accept / Cancel changes like a dataset?
Thanks!
I finally figured it out:
a) While I am able to save data without any issues, the app throws error only when I try to "rebind" it. As the grid and controls are already bound to datasource, which contains latest changes, I removed the rebinding code and now it no longer throws any error ("Operation is invalid"
b) Undoing changes - this was accomplished by calling BindingSource.CancelEdit() when user does not want to save changes.
Related
There are two user controls
1. First User control, having only Datagrid views only
2. Second User control, having the Details & operations like addition, modification & deletion button.
Both the user control will be placed in the Main window.
Whenever i am selecting a row in the grid particular data is displayed in the second user control (Using PRISM eventAggregator). But when adding or updating or deleting it is not happening updating the first user control data grid. How to achieve this.
Use the ObservableCollection it represents a dynamic data collection that gives a notification when items get added or removed.
If you are already using this which is not clear from your question, then you should check if you are using the INotifyChanged correctly or give us some code so we can help you.
But with the ObservableCollection and the INotifyChanged you should be able to solve your own question.
I have a winforms app with a group of text boxes for an entity, let's call the entity Product
One of my textboxes is hidden because it holds a foreign key to another database object Business Unit. I populate the hidden textbox using a combobox that looks up values on the parent table. When the selection changes, so does the value in the hidden textbox.
private void businessUnitComboBox_SelectionChangeCommitted(object sender, EventArgs e)
{
this.businessUnitIdTextBox.Text = this.businessUnitComboBox.SelectedValue.ToString();
this.businessUnitComboBox.Focus();
}
Problem is after calling SaveChanges on my context, this change on the hidden textbox is not persisted. Oddly, if I update any of the other Product textboxes, they save just fine.
The textboxes were added to the project with standard drag and drag drop from Visual Studio's GUI, and a bindingsource was created when adding automatically.
My entities implement INotifyPropertyChanged using fody-propertychanged.
I am struggling to find the issue here. Creating new records works fine, but updating that one Foreign Key value never has.
Is this because it is a navigation property and needs to be handled differently, or are there other possibilities as to why the changes are not persisted? Any help is much appreciated.
May be it is because of lacking of this.Validate() in your Save method.
Put this code in save method of your form.
this.Validate();
this.yourBindingSource.EndEdit();
yourDbContext.SaveChanges();
The default behavior of data binding is OnValidation.
To change this behavior you can go to your textbox properties, open databinding part, open advanced, Change Data Source Update Mode to OnPropertyChanged.
So the code of your databinding in designer generated codes will be like this:
this.nameTextBox1.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.productBindingSource, "Category.Name", true, System.Windows.Forms.DataSourceUpdateMode.OnPropertyChanged));
In this case you don't need to use this.Validate before saving changes and you can simply use
this.yourBindingSource.EndEdit();
yourDbContext.SaveChanges();
I have form in which I can change the name of stations in my database (SQL Server: in table Stations).
To choose which station I want to edit I've used a combobox.
What I want to happen now is that when I update a station, my ComboBox with stations immediatly gets updated with the edited station.
Is there a way to do this or is this impossible?
I am assuming that this is a Windows Forms application based on the subject. If this is the case, then you have most likely performed databinding, perhaps using a DataTable or a collection, to the ComboBox.
If this is the case, and the object that you have databound supports System.ComponentModel.IBindingList, then all you need to do is add or update the record in the underlying object.
When this happens, the object that has implemented IBindingList sends a message out to any controls that are "listening" and tells them what just happened. The control will then update their data/user interface to reflect these changes.
I got a datagrid(dg) which is bound with an observable collection of POCO [Name(string), value(int), isReady(bool)]
I need to let the name being editable so my DataGrid has <data:DataGridTemplateColumn.CellEditingTemplate> wich contains a TextBox.
when committing the edit, I need to call a WCF Service to validate the name. That's what I am doing in CellEditEnded.
But when the name is not valid, how can I:
Display an error on the datagrid (searching a solution with ValidatesOnNotifyDataErrors but can't succeed)
Put the cell back in edit mode.
Here's why I can't validate in the POCO:
DataGrid is in edit mode
By double clicking on a cell, the label containing the data becomes a TextBox. I'm now in edit mode
I insert an error. An assynchronous validation is launched. DataGrid is back in display mode
the assync is finished, I raised my error, but nothing happens (visually I mean) because ValidatesOnNotifyDataErrors does not seems to work on label.
And more, as I know there is an error, if I enter back in edit mode, I get a really strange display telling there is 1 Error and when enterring this state, I can't get out of edit mode, whatever I do...
Thx
(sorry for my bad english)
I would recomend that you do not use the grid event to validate your data - do this on your POCO properties setter - its cleaner, easier to mantain and if you use this POCO with another control your validation will still work.
Since you need to access a service to validate the value, your best bet is to implement the interface INotifyDataErrorInfo, that allows you to make asynchronous validation, look here: http://weblogs.asp.net/fredriknormen/archive/2009/11/22/silverlight-4-and-asynchronous-validation-with-inotifydataerrorinfo.aspx and here http://www.silverlight.net/learn/data-networking/validation/asynchronous-data-validation
I have simple SL user control. A listbox which shows all customers and on the right a number of textboxes and comboboxes that are bound to the SelectedItem (Customer) in the listbox. The SelectedItem bound to SelectedCustomer property.
I am looking for a pattern/methodology to deal with canceling changes made to the customer (in the bound textboxes and combo boxes).
The edit controls (textboxes and combo's) can be one or two way bound to the selecteditem of the listbox.
If they are two way bound, immediate changes in the textboxes are reflected in the listbox. If they are oneway bound the changes in the textboxes are not reflected in the SelectedCustomer object.
At the bottom of the edit form i have typical Save, Cancel, Delete buttons. The save button for instance would take the SelectedCustomer object (if twoway bound and I would send through service for saving on server).
If the textboxes are one way bound i have to capture somehow the textbox values and insert into some object for sending to the server for saving.
If I use twoway binding , and say the save operation fails...i have to set the SelectedCustomer values back to original values otherwise the client now continues to see data that has not been saved.
There must be an easy way of dealing with this type of scenario....
RIA Services with Entity Framework already provides this functionality, basically how RIA services works and you can do it too as follow.
Each class implements interface called IEditableObject, which provides methods BeginEdit/EndEdit and CancelEdit. And it also stores (copies) instance of same class with name "OriginalEntity" with same values that were loaded from server.
After the form shows up for user to modify, BeginEdit is called, which probably caches every property using reflection, in some sort of dictionary. If you call CancelEdit, then values from OriginalEntity are loaded back in object.
Upon some errors while saving changes, you can either refresh the entities from server (best way) or you can try loading properties back from OringalEntity property.
I wouldn't discard user changes, as that easily leads to user frustration. IMHO, the user should not be informed about connection problems by uncontrolled data rollbacks.