Combobox – assigning the DataSource property causes SelectedIndexChanged event to fire

  • Thread starter Thread starter GiJeet
  • Start date Start date
G

GiJeet

Hello, I have a datatable and when I assign it as the datasource to a
combo box it causes the SelectedIndexChanged event to fire - obviously
I only want SelectedIndexChanged event to fire when the user makes a
change.

See: http://geekswithblogs.net/mnf/archive/2005/10/25/57979.aspx

So, I want to use the combobox.Items.AddRange() method or just Add()
method instead. However, I need both the DisplayText and DisplayValue
(not shown) to be added. The DisplayText is a string and the
DisplayValue needs to be an int (a PK from the table)

What is the best way to do this?
a) convert the datatable into datarows?
b) loop thru the datatable and create a collection? which one holds
two different value types?
c) linq ?

I would appreciate some example code as I learn better from seeing
code.

TIA,
G
 
Hello, I have a datatable and when I assign it as the datasource to a
combo box it causes the SelectedIndexChanged event to fire - obviously
I only want SelectedIndexChanged event to fire when the user makes a
change.

Create a Boolean field called _changedByCode or something like that. Set it
to true when you're about to assign the DataSource. Then, in the event
handler for SelectedIndexChanged, only perform the normal processing when
this bool is false. That way the event will still execute but you won't do
the stuff that you only want to do in response to a user action.

And of course remember to set that bool back to false after assigning the
DataSource.

For more complex operations, you might consider making a helper class that
keeps track of how many times your code has said "I'm doing this through
code" so that you can nest these calls. Basically creating a little stack.
 
Create a Boolean field called _changedByCode or something like that. Set it
to true when you're about to assign the DataSource. Then, in the event
handler for SelectedIndexChanged, only perform the normal processing when
this bool is false. That way the event will still execute but you won't do
the stuff that you only want to do in response to a user action.

And of course remember to set that bool back to false after assigning the
DataSource.

For more complex operations, you might consider making a helper class that
keeps track of how many times your code has said "I'm doing this through
code" so that you can nest these calls. Basically creating a little stack..

good idea. I'll try that. thanks!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top