combobox woes

S

Shaun Wilde

Hi all

I've been trying to make my wee app a bit faster and and I am seeking to
reduce the time it takes to load my data into the combo boxes.

Now I have a search that is quite expensive but is used by 5 comboboxes on
the same form.

However I hit a snag. If I use the same Dataset for the datasource for each
combobox then all the comboboxes become linked - ie if you change one you
change then all.

At first I thought it was DataSets so I tried it with my own ArrayList but
no it seems that this is what happens - I don't understand the rational at
all behind this 'designed' behaviour but it is not what I expected. This
behaviour is seen on both Compact and Full .NET frameworks and in C# and
VB.NET.

So I thought about cloning my DataSet so I can get it once and use clones
for each control - but no MS have taken away the .Copy method from DataSet
and the only way I can find to make a copy is via XmlReader and is more time
consuming than the search (along the lines of "select a,b from x order by
a") which was surprising.

Now in the Full Framework when we load DataSets we have a BeginInit/EndInit
combo which does the old WM_SETREDRAW message to the combobox to disable
updating but again this method has been pulled (so I'll have to spend time
working out if I can do this natively - though how I'll find my combobox
will be a miracle - I may just have to blanket bomb them all on a form).

So I've tried pulling my control off the form - loading it - and then
putting it back - as mentioned in a past newsgroup posting (not convinced
yet)

I tried not using a DataSet but make my own collection of ItemPair objects
while iterating using a SqlCeDataReader (some improvement) and then loading
that as the DataSource

I tried not using DataSource property but assing them to the array directly
using Items.Add(...) but the ValueMember property is ignored. :(

So does anyone have a fast DataSet copier or code they use to see if I am
doing something daft?

Can someone suggest a better way of loading a combobox with >100 items?

Can anyone explain why using the same array for a common datasource exhibits
the described behaviour? (see code below)

Thanks

Shaun

PS - code follows
Dim ar As New ArrayList

ar.Add(New ItemPair("String 1", 1))

ar.Add(New ItemPair("String 2", 2))

ar.Add(New ItemPair("String 3", 3))

ar.Add(New ItemPair("String 4", 4))

ar.Add(New ItemPair("String 5", 5))

ar.Add(New ItemPair("String 6", 6))

ComboBox1.DisplayMember = "First" ' the name of the property which contains
the string data

ComboBox1.ValueMember = "Second" ' the name of the property which contains
the int data

ComboBox1.DataSource = ar

ComboBox2.DisplayMember = "First"

ComboBox2.ValueMember = "Second"

ComboBox2.DataSource = ar

ComboBox2.SelectedValue = 3 ' both combo boxes focus on the same element
 
S

Shaun Wilde

Peter

DataTable.Clone only does structure (ie schema) according to the docs (just
like DataSet.Clone) it doesn't do data - which is what the Copy methods
would do if they existed.

I'm going to try using ImportRow on the DataTable to duplicate my data.

I'll try the properties on the ComboBox control to see if that helps speed
things up

Shaun

Peter Foot said:
While the DataSet may lack a Clone method the DataTable does have a Clone
method so you can clone an individual table in your Set e.g.

DataTable dt2 = ds1.Tables[0].Clone();

comboBox1.DataSource = dt2;

To speed up population of the ComboBox you could set its enabled and visible
properties to false while the list is populated.

Peter

--
Peter Foot
Windows Embedded MVP

In The Hand
http://www.inthehand.com

Shaun Wilde said:
Hi all

I've been trying to make my wee app a bit faster and and I am seeking to
reduce the time it takes to load my data into the combo boxes.

Now I have a search that is quite expensive but is used by 5 comboboxes on
the same form.

However I hit a snag. If I use the same Dataset for the datasource for each
combobox then all the comboboxes become linked - ie if you change one you
change then all.

At first I thought it was DataSets so I tried it with my own ArrayList but
no it seems that this is what happens - I don't understand the rational at
all behind this 'designed' behaviour but it is not what I expected. This
behaviour is seen on both Compact and Full .NET frameworks and in C# and
VB.NET.

So I thought about cloning my DataSet so I can get it once and use clones
for each control - but no MS have taken away the .Copy method from DataSet
and the only way I can find to make a copy is via XmlReader and is more time
consuming than the search (along the lines of "select a,b from x order by
a") which was surprising.

Now in the Full Framework when we load DataSets we have a BeginInit/EndInit
combo which does the old WM_SETREDRAW message to the combobox to disable
updating but again this method has been pulled (so I'll have to spend time
working out if I can do this natively - though how I'll find my combobox
will be a miracle - I may just have to blanket bomb them all on a form).

So I've tried pulling my control off the form - loading it - and then
putting it back - as mentioned in a past newsgroup posting (not convinced
yet)

I tried not using a DataSet but make my own collection of ItemPair objects
while iterating using a SqlCeDataReader (some improvement) and then loading
that as the DataSource

I tried not using DataSource property but assing them to the array directly
using Items.Add(...) but the ValueMember property is ignored. :(

So does anyone have a fast DataSet copier or code they use to see if I am
doing something daft?

Can someone suggest a better way of loading a combobox with >100 items?

Can anyone explain why using the same array for a common datasource exhibits
the described behaviour? (see code below)

Thanks

Shaun

PS - code follows
Dim ar As New ArrayList

ar.Add(New ItemPair("String 1", 1))

ar.Add(New ItemPair("String 2", 2))

ar.Add(New ItemPair("String 3", 3))

ar.Add(New ItemPair("String 4", 4))

ar.Add(New ItemPair("String 5", 5))

ar.Add(New ItemPair("String 6", 6))

ComboBox1.DisplayMember = "First" ' the name of the property which contains
the string data

ComboBox1.ValueMember = "Second" ' the name of the property which contains
the int data

ComboBox1.DataSource = ar

ComboBox2.DisplayMember = "First"

ComboBox2.ValueMember = "Second"

ComboBox2.DataSource = ar

ComboBox2.SelectedValue = 3 ' both combo boxes focus on the same element
 

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

Top