469,292 Members | 1,349 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,292 developers. It's quick & easy.

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

Post your reply

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

Similar topics

1 post views Thread by Stephen.Hunter | last post: by
1 post views Thread by Kevin Hodgson | last post: by
2 posts views Thread by FaWiizio | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
1 post views Thread by Geralt96 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.