PC Review


Reply
Thread Tools Rate Thread

Combobox – assigning the DataSource property causes SelectedIndexChanged event to fire

 
 
GiJeet
Guest
Posts: n/a
 
      30th Apr 2009
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/archiv.../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
 
Reply With Quote
 
 
 
 
Jeff Johnson
Guest
Posts: n/a
 
      30th Apr 2009
"GiJeet" <(E-Mail Removed)> wrote in message
news:5fd6f33a-8163-48ff-977a-(E-Mail Removed)...

> 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.


 
Reply With Quote
 
GiJeet
Guest
Posts: n/a
 
      30th Apr 2009
> On Apr 30, 12:48*pm, "Jeff Johnson" <i....@enough.spam> wrote:
> 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!
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Dropdown list - SelectedIndexChanged event doesn't fire =?Utf-8?B?SmFjaw==?= Microsoft ASP .NET 1 14th Mar 2005 04:27 PM
SelectedIndexChanged event does not fire? Jim Microsoft ASP .NET 3 8th Mar 2005 05:46 AM
DropDownList not fire event SelectedIndexChanged Gonçalo Boléo Microsoft C# .NET 2 11th Jan 2005 03:34 PM
SelectedIndexChanged event in comboA causes same event to fire in comboB moondaddy Microsoft Dot NET Framework Forms 5 29th Sep 2004 04:04 AM
When does SelectedIndexChanged event fire & an alternative to this event? moondaddy Microsoft Dot NET Framework Forms 2 1st Nov 2003 07:09 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:40 AM.