My own question got me thinking ... I needed an event that fires before list changes and another event which fires after list changed. I could not find any event in the dropdownlist which fires after SelectedIndexChanging when new item is added to the list. But, I found out that BindingSource object has an event called ListChanged which seems to be firing after list has changed and after SelectedIndexChanging was fired. RadDropDownList has an event ItemDataBinding which seems to be firing before list changed. I am not sure if these are the best events to use for this solution, but it seems to be working for now. The only limitation, is that DataSource has to be set to BindingSource object and not to the list directly.
So, below is my code that I've come up with. Maybe someone can make a suggest if there are any potential issues that may come up and/or a better way of doing this to make this more universal for any object that is assigned to DataSource.
public
class
RadDropDownListExtended : RadDropDownList
{
private
bool
_DataBindingChanging;
//need this method to ensure that I bind to BindingSource and not directly to the list
public
void
SetDataSource(BindingSource bindingSource)
{
//remove list changed event in case binding changes
if
(
base
.DataSource !=
null
&&
base
.DataSource.GetType() ==
typeof
(BindingSource))
((BindingSource)
base
.DataSource).ListChanged -= Bs_ListChanged;
base
.DataSource = bindingSource;
bindingSource.ListChanged += Bs_ListChanged;
//hook up to an event which fires after item was added to the list
ItemDataBinding += drpTest_ItemDataBinding;
// hook up to an even which seem to fires when list is about to change
SelectedIndexChanging += drpTest_SelectedIndexChanging;
//hook up to an event which will cancel the change of the selected item which should fire between other 2 events
}
private
void
Bs_ListChanged(
object
sender, ListChangedEventArgs e)
{
_DataBindingChanging =
false
;
}
private
void
drpTest_SelectedIndexChanging(
object
sender, Telerik.WinControls.UI.Data.PositionChangingCancelEventArgs e)
{
e.Cancel = _DataBindingChanging;
}
private
void
drpTest_ItemDataBinding(
object
sender, ListItemDataBindingEventArgs args)
{
_DataBindingChanging =
true
;
}
}