473,405 Members | 2,262 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,405 software developers and data experts.

C# - How to Make the ComboBox ' Values Distinct (Unique)?

Hi,

I have a comboBox that controls a datagridview.
I would like to know how I can make the values displayed in the combobox unique (distinct)?

Thank you for your help,
Jul 5 '07 #1
6 18518
Plater
7,872 Expert 4TB
Since we know almost nothing about what you want, I can only offer this:
Before adding a value to the combobox, check to make sure it's not already in there.
Jul 5 '07 #2
Since we know almost nothing about what you want, I can only offer this:
Before adding a value to the combobox, check to make sure it's not already in there.
My combobox is linked to a column of a table.
What I'd like is the combox not to display duplicates.
Jul 5 '07 #3
TRScheel
638 Expert 512MB
Since we know almost nothing about what you want, I can only offer this:
Before adding a value to the combobox, check to make sure it's not already in there.
Offtopic, and quite informally.... LOL


For our little buddy above. If you are using c#/vb code to make this combo box, and its not dynamically created through javascript or some other script language, you can do it on page load after the object is made by iterating through each item in the combobox and seeing if the item is already there, deleting the item if it is. Something like:

Expand|Select|Wrap|Line Numbers
  1. for(int i = 0; i < comboBox.Items.Count; i++)
  2. {
  3.     for(int y = 0; y < comboBox.Items.Count; y++)
  4.     {
  5.          if( y != i && comboBox.Items[i].Text == comboBox.Items[y].Text)
  6.          {
  7.               comboBox.Items.RemoveAt(i);
  8.               break;
  9.          }
  10.     }
  11. }
  12.  
Jul 5 '07 #4
...iterating through each item in the combobox and seeing if the item is already there, deleting the item if it is. [/code]
Hi,
Thank you for your reply.
I am getting the following error message when I run the program:
An unhandled exception of type 'System.ArgumentException' occurred in System.Windows.Forms.dll

Additional information: Items collection cannot be modified when the DataSource property is set.
?
Jul 5 '07 #5
Plater
7,872 Expert 4TB
That error gets thrown when trying to modify a control's dataset directly that is bound to a datasource.
You will have to edit the datasource to do this.
What I recomend is creating your datasource (I'll use a DataTable as an example).
Populate it with all your information.
Then make a copy of it.
Then edit the copy so it contains no doubles.
Use the copy on your combobox and the original on you grid
Jul 5 '07 #6
TRScheel
638 Expert 512MB
That error gets thrown when trying to modify a control's dataset directly that is bound to a datasource.
You will have to edit the datasource to do this.
What I recomend is creating your datasource (I'll use a DataTable as an example).
Populate it with all your information.
Then make a copy of it.
Then edit the copy so it contains no doubles.
Use the copy on your combobox and the original on you grid
I like this solution
Jul 6 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Stephen.Hunter | last post by:
Hello Everybody (anybody)? I am trying to appent a combobox to include data entered by a user which is Not In List. I have came accross this code from Allen Browne which seems to be what I am...
1
by: Kevin Hodgson | last post by:
I have a DataBound ComboBox, which has a text value as the Value property, and the UniqueID for that value is bound to the Combobox.Tag property. When a user makes a new selection in the...
2
by: FaWiizio | last post by:
Is it possible to add an item to a combobox this way: ComboBox1.Items.Add("Cool!") AND assign to this new Item a unique index ? As in HTML <option value="1">Cool</option>
3
by: fedya | last post by:
I am trying to have the last 12 months to always be the option in the dropdown for a combo box. (Basically a combobox, with dynamic options) I am using Access 2000. What is the function and...
0
by: NeoGeo | last post by:
I have a problem with a SQL SELECT query. As far as my research goes i figured out that UNIQUE is used when you have one column that you whant unique and DISTINCT is used when you have more than one...
6
by: shira | last post by:
Hi, Looking to see if someone might have an explanation for this behavior. Is it a bug? Corruption? I have been able to reproduce the problem with only 2 rows and 1 field. Here is the table:...
2
by: nkumarin001 | last post by:
Hi, I have some doubts regarding NULL values. Can any one help me in clearing my doubts. I have created a table:- create table suppliers ( supplier_id number, supplier_name...
1
by: The.Daryl.Lu | last post by:
Hi, two parts to my problem if someone can help address either one or both: 1. I want to SELECT everything in the table if it matches the criteria when the query button is pressed (this is just...
14
by: Mark | last post by:
I have a table with a field that uses a combobox to populate values. The Lookup tab within table design mode is the following: Display Control Combo Box Row Source Type ...
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: 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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.