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
Related
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 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.
I had hoped that the EndEdit() method would take all bindings and write the control values values back to the datasource. Suprisingly, it doesn't always work. Sometimes the values are not written back to the datasource.
When I want to ensure that the controls write their data back to the datasource, is my only option to get each control's databinding and call WriteValue()?
Any other thoughts or ideas?
UPDATE
I bind to an object.
I have a textbox and I handle the TextChanged event. Within this handler I call EndEdit().
When I bind to the datasource, the TextChanged event is called which triggers the EndEdit(). If I then type into the textbox, the FIRST call to EndEdit() has no effect. After that it seems to work.
Note: I can get this to work if I do not have a TextChanged handler until AFTER the data is bound to the TextBox. It seems that the first EndEdit call (during the binding) seems to have a bad effect.
EndEdit() only works if binding source is in editing state, which is usually triggered by entering into control or starting actual editing.
I have a usercontrol that has custom unbound data columns in a gridView. I want to retain view state when switching between screens in my app in terms of selected tabs and focused rows etc.
When I do this switch and restore that view state, my custom data is gone and the CustomUnboundColumnData event handler does not get used at all.
Is there a way to raise the CustomUnboundColumnData event for this gridView after I restore my view state?
Call the RefreshRow or RefreshRowCell methods, passing a row handle and optionally a column.
If you are using the ASPxGridView, I would suggest that you turn off the EnableRowsCache property. One more solution is to call the ASPxGridView's DataBind method explicitly within the Page_Init method.
Found a work around. I was refreshing my data in this grid view anyway. However, the few columns that had unbound data needed to be re-added to the grid before refreshing this data.
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.