Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Data bound ComboBox

Posted on 2004-04-15
6
Medium Priority
?
389 Views
Last Modified: 2007-12-19
I have a combo box on a pocket pc form with the data source set to a DataTable and the ValueMember set to an ID field and the DisplayMember set to a Description field.

To get a blank entry at the top of the combo box I add a row to the beginning of the DataTable with an ID of 0 and a description of spaces.

Sometimes i need to select a specific value when loading the combo box so it displays the correct item on entry. I do this as follows:
this.combobox.SelectedValue = ID (where ID > 0)

If there are 2 or more rows in the DataTable (before i add a blank one) it works fine.
However, if there is only 1 row in the DataTable an unspecified exception is thrown.

Any idea why, or how this can be avoided???
0
Comment
Question by:MartinLarge
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
6 Comments
 
LVL 6

Expert Comment

by:DaniPro
ID: 10833683
You must verify that the number of the itemes is less then your id

if (combobox.Items.Count > ID)
      combobox.SelectedValue = ID;
0
 

Author Comment

by:MartinLarge
ID: 10834030
I have an Id with a value of 8 when there are only 3 rows in the table and it still works. It doesn't work if there is only 1 row in the original table.

What you suggested would be OK if using SelectedIndex but i'm using SelectedValue from a datasource.
0
 
LVL 6

Expert Comment

by:DaniPro
ID: 10834306
You have right (it was SelectedIndex), then you must verify that exists an item with the ValueMember with your ID:

if (combobox.Items.Contains(ID))
     combobox.SelectedValue = ID;
0
 

Author Comment

by:MartinLarge
ID: 10834693
The Id does exist within the DataTable.

If the DataTable has 2 rows the combo box works fine and selects the correct id.
If i remove the row which does not match the id it crashes.

I think there is a problem with adding the blank datarow to the DataTable.
When i don't do this it works fine.
0
 

Accepted Solution

by:
ee_ai_construct earned 0 total points
ID: 11756969
PAQed, with points refunded (125)
ee_ai_construct - CS Mod
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question