Monday, January 31, 2005

Manipulating Binded Datasource at runtime is tricky..

Its not that easy as we think to manipulate a collection that is bound to a combobox/listbox. This works out of the box if you are using a DataView but if you are using something like an ArrayList to bind you comboBox control, then the changes in the binded collection will not directly update the control collection.

However you can force this by calling SuspendBinding() and ResumeBinding() on the BindingContext. You might need to do something like this everytime you manipulate the collection..

{

// code...

BindingManagerBase bindingManagerBase = BindingContext[_documentTypeDTOList];

bindingManagerBase.SuspendBinding();

_documentTypeDTOList.Add(documentType); // manipulating and adding a new type at run
time.
_documentTypeComboBox.DataSource = __documentTypeDTOList;

_documentTypeComboBox.DisplayMember = "Name";

bindingManagerBase.ResumeBinding();

}

6 comments:

Premanand C said...

It might be safer to envolope the code after the SuspendBinding in a try block and put the ResumeBinding in a finally block. That way, if an exception happens after the call to SuspendBinding, the system is not left in an inconsistent state. So, the code becomes:
{
// code...

BindingManagerBase bindingManagerBase = BindingContext[_documentTypeDTOList];

bindingManagerBase.SuspendBinding();
try
{
_documentTypeDTOList.Add(documentType); // manipulating and adding a new type at run
time.
_documentTypeComboBox.DataSource = __documentTypeDTOList;

_documentTypeComboBox.DisplayMember = "Name";
}
finally
{
bindingManagerBase.ResumeBinding();
}

}

Premanand C said...
This comment has been removed by a blog administrator.
Barnsley-Freeads said...

If you want a seriosly good laugh watch these hillariously funny videos
http://watchthisfunnyvideonow.com/taf/?x=23077XlRf Humerous

Anonymous said...

I found this site using [url=http://google.com]google.com[/url] And i want to thank you for your work. You have done really very good site. Great work, great site! Thank you!

Sorry for offtopic

Anonymous said...

how viagra works non prescription viagra cialis vs viagra viagra no prescription watermelon viagra viagra uk cost pill buy viagra online uk pfizer viagra women does viagra work cheap viagra nz viagra no prescription online viagra buy viagra in england viagra overnight

Anonymous said...

Without prescription zantac 50mg tamiflu Side-effects lasix World delivery atarax Pharmacy geriforte Sale coreg