473,385 Members | 1,317 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Combo Box, finding the right Item

I have a Combo box, populated from a List<Objects>.

The Object has a .name, and .id property. The combo box has been
populated with these using DisplayMember and ValueMember.

When I load the screen, and am editing an item, the combo must go to
the correct 'Manufacturer' that the main screen wants to see. This is
based on a Model object I have. The model has a manufacturer.

So, I populate the combo with the possible Manufacturers, but then, I
need to default the combo to the one that the main screen needs. The
code below does that, by iterating through the items in the combo.
However, I am sure there is a better way to do it. I did it the other
day in a test app, and it worked. But for the life of me, I can't get
it to work now.

The code below DOES work, but could someone tell me the easier/correct
way? I don't want to use data binding right now. I just want to move
the index to the correct item.
private void frmAddEditModel_Load(object sender, EventArgs e)
{
List<IManufacturermanufacturers =
doManufacturers.GetListOfManufacturers();
cmbManufacturer.DisplayMember = "name";
cmbManufacturer.ValueMember = "id";
foreach (IManufacturer man in manufacturers)
{
cmbManufacturer.Items.Add(man);
}

if (ModelID 0) // Are we editing?
{
IModel model = doModels.GetModel( ModelID );

ebModel.Text = model.model;
ebEdition.Text = model.edition;

int cmbIndex = 0;
foreach (IManufacturer man in cmbManufacturer.Items)
{

if (man.id == model.manufacturer)
{
break;

}
cmbIndex++;

}
cmbManufacturer.SelectedIndex = cmbIndex;
if (model.deleted == true)
{
tsStatus.Text = "Model is marked as deleted.";
}
else
{
tsStatus.Text = string.Empty;
}

}

}

Jun 2 '07 #1
4 1716
the easier way? you want the faster way?
Jun 2 '07 #2
u can use caching maybe for the faster access or you can use dataview
component.u can set the rowfilter property of the dataview
component...
Jun 2 '07 #3
On 2 Jun, 15:11, Hakan Fatih YILDIRIM <hfysilis...@gmail.comwrote:
the easier way? you want the faster way?
I'm sure I saw a way that meant no itterating through the items.
So, yes, the faster way.

Jun 2 '07 #4
Instead of this code:

On Sat, 02 Jun 2007 06:42:35 -0700, Cralis <ad***@myschoolmates.comwrote:
int cmbIndex = 0;
foreach (IManufacturer man in cmbManufacturer.Items)
{

if (man.id == model.manufacturer)
{
break;

}
cmbIndex++;

}
cmbManufacturer.SelectedIndex = cmbIndex;
Try "cmbManufacturer.SelectedValue = model.id".

One thing to keep in mind is that the SelectedValue property doesn't work
unless you've added your data to the ComboBox by building an ArrayList and
then setting the ComboBox.DataSource property to that ArrayList. I don't
know why it doesn't work when you simply put the object into the Items
collection for the ComboBox, especially since the DisplayMember property
does work in that case. But it doesn't. :(

Sure seems like it should. I wonder if anyone else has a good explanation
for the discrepancy.

Anyway, that means that this code:
foreach (IManufacturer man in manufacturers)
{
cmbManufacturer.Items.Add(man);
}
Should read instead:

ArrayList rgman = new ArrayList();
foreach (IManufacturer man in manufacturers)
{
rgman.Add(man);
}
cmbManufacturer.DataSource = rgman;

Pete
Jun 2 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Daniel Hill | last post by:
OK, I have very, VERY basic background knowledge of VB6 and have now upgraded to VB.NET and now I'm struggling to bring up the forms I want. What I am looking to do is to have a click a command...
4
by: Yuk Cheng | last post by:
<<<start index.htm>>> <html> <head> <script> function perform(action){ } </script> </head>
2
by: A | last post by:
I have a combo box that is databound to an ArrayList (IList to be more exact) I have a situation where if the user cancels a save I want the combo value to go back to blank. This should be...
14
by: Gary Shell | last post by:
I have five comboboxes each has a item listed populated by hand in the IDE. Each has the text property bound to a text field in the database. When I initially fill the dataset and display the page...
9
by: Kay | last post by:
Hi all, Could you tell me the best way to add a blank item(as first item) in a data binded combo box? Because I think I didn't do it right and it generate an error if the second item(after the...
1
by: HDMorales | last post by:
I have a combo box on a form that displays a list of a text field with item numbers. All items start with a number and then alpha characters. When I display the list on the combo box and try to...
2
by: sara | last post by:
I can't find this in a post - I am not sure how to describe and search, so here goes: I have a combo box for the user to choose and existing vendor (there are about 1200 vendors). The user can...
6
by: Smokey Grindle | last post by:
Say I have a combo box with the following simple object Public class MyObject public ID as integer public Name as string public overrides sub ToString() as string return name end sub end...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.