I have a Silverlight datagrid that will change its data source dynamically at runtime, and the datagrid has a SelectionChanged event handler. But I don't want the SelectionChanged event handler to be fired every time the data source is changed. Is there a easy way to prevent this happening?
Thanks,
Wei
You can certainly unload (-=) the event handler and then load it back (+=) after you change the datasource. That should work fine. I would like to offer a suggestion, however. Instead of wiring up the SelectionChanged, does the Silverlight DataGrid have a SelectedItem property that you can bind to instead? If so, then you can bind that to a property in a class and listen for PropertyChanged on that property. Then you can lose the event handler entirely. That sets you up better for unit testing and is more in line with an MVVM philosophy.
Related
DataGrid has 2 events:
ASP.NET DataGrid:
ItemCreated
ItemDataBound
When you bind to a data source ItemCreated gets fired followed by ItemDataBound.
I need to know if anyone can think of any good reason of using ItemCreated.
I can't find anything versus putting the whole code in ItemDataBound event(other than keep the event handler's code smaller).
Please let me know if you think otherwise.
ItemCreated is fired on the postbacks, but ItemDataBound only during databinding.
ItemCreated is fired before the data bind actually happens. You would normally put code dealing with the appearance and non bound content of the grid in this event.
ItemDataBound is fired after the data bind. You would normally put code dealing with the data here.
ItemCreated is basically there for you to interact with UI things, and yes, you can do the exact same thing on the ItemDataBound event as well/create your UI changes there too... i prefer DataBound because then the data is already there, but i understand the purpose of ItemCreated
I have a form with 1 bindingsource control.
A grid and several standalone controls (texts and labels) are sharing the binding with this source.
Every time a user changes the grid row i want to enable/disable some controls.
Should i write this code in BindingSource.CurrentChanged event or in DataGridView1.RowValidating event?
I tend to use the bindingsource event as i think it gives me globally what i want, but i "feel" its wrong to do UI code in there.
Opinions?
It is not wrong to use BindingSource.CurrentChanged evnt to handle UI code. BindingSource is a WinForms component.
The dataGridView.RowValidating event fires before BindingSource event.
Depending an what you need, I'd say it ok to use anyone of them :)
I would like to know if it is possible to make a difference in the event selection change of a combobox.
I want to make a difference between a user that manually click the combobox and changes its value and a change in the selection that i do from code.
ie :
if i click my combobox and change its value by hand, the event is fired
but if i do myCombobox.selectedItem=1 [edit] the events is not fired
Is there an event that has this behaviour in a wpfcombobox ?
If not, do you have any idea on how to do that ?
Thanks
[edit] or if it is the binding of the combobox that changes its value
You are dealing with a couple of different scenarios, both of which are solvable.
1) Don't process SelectedItem requests during databinding. You have at least two options here:
a) Don't add the event handlers to the control until after databinding is complete or the form is loaded (depending on whether or not databinding is automatic or manual).
b) Set a form level property indicating when it is ok to process the SelectedItem event. You will probably want to set this to true after the form is loaded or after the databinding is complete. In your SelectedItem code, don't perform any actions unless this property is true.
2) Process the SelectedItem logic if the SelectedItem is changed programatically. Again, two options:
a) Extract your logic from the SelectedItem event into a method and then call this method when you perform the logic to set the selected item.
b) Create a custom combobox that inherits from the base and add a SetSelectedItem method (for example) to this inherited combo. This method would then raise the SelectedItem method. This would be reusable and you wouldn't have to remember to do 2 pieces of work whenever you set the SelectedItem manually.
Before you change the selecteditem in code, remove the event handler with -=, then add it back afterwards.
How can I have a property grid update automatically when the object in its SelectedObject property changes? I've tried implementing INotifyPropertyChanged in my class but the property grid does not actually show the new propertyies of the object in the background until I click on it.
I've tried subscribing to the PropertyChanged event of my object directly, and calling the Refresh() method of the PropertyGrid when it is envoked. But some of my properties are related. Meaning changing one property may evoke multiple PropertyChanged events. This seems to work fine, but I'm still wondering if there is a cleaner way of doing this through DataBinding. Also I'd like to avoid having the control Refresh multiple times after the user only updated a single property.
So is there a way to get the PropertyGrid to refresh from PropertyChanged events?
Try adding the RefreshProperties attribute to each property that has dependencies:
[RefreshProperties(RefreshProperties.All)]
Now, each time this property changes - it will automatically refresh the other fields.
This is a much cleaner approach than calling the property-grid's "Refresh()" each time.
I don't know if there's a built-in way to do it, but here's a suggestion if you want to avoid multiple calls to Refresh for related properties :
When a PropertyChanged event occurs, start a timer. If the event occurs again before the timer has elapsed, do nothing. In the Tick event of the timer, refresh the PropertyGrid and stop the timer
I am having a DataGrid in one usercontrol which is getting filled with some data when I press some button(ouside it). This Datagrid is filled by one linkbutton also. When I click this LinkButton then OnItemCommand or SelectedIndexChanged should fire, but both the events are not firng. While the control's PageLoad event is firing.
Please let me know where I am doing the mistake.
Thanks
Is your control added dynamically, using something like Page.LoadControl()? If so, events won't fire unless the control is dynamically added for each request. That's the only way the control tree can be properly built again. I usually do this in Init().
https://web.archive.org/web/20211029043701/https://www.4guysfromrolla.com/articles/042402-1.aspx
For those that see this post, looking for an answer. Most likely, you're rebinding the data to the datagrid on every call. Use a conditional and check !this.IsPostBack.