Solved

Does DataGridViewComboBoxColumn support duplicated names??

Posted on 2014-09-03
8
271 Views
Last Modified: 2014-09-05
Hi experts
I wonder if DataGridViewComboBoxColumn is meant to support duplicated names.
eg. the Personality of 'Person B" is the 3rd 'Average' in the drop-down selections.
The top screen showed that when I clicked the drop-down for the first time, it correctly pointed to the 3rd item.
After I'd clicked elsewhere, I clicked the drop-down again (as in bottom screen), it now pointed to the 1st item incorrectly.
screenshot of problemWhen I debugged the code, I noticed that somewhere (don't know where), the data member was internally modified to point to the 1st item. I've no idea where and how it did it.
The code is as follows. I deliberately replaced all the binding source and binding list in order to avoid anything happened behind the scene.

The form:
 public partial class Form1 : Form
    {
        private readonly List<Person> _peopleList = new List<Person>()
                                                            {
                                                                 // ( Person name, Internal identifier)
                                                                 new Person("Person A", PersonalityCode.Y),
                                                                 new Person("Person B", PersonalityCode.Z),
                                                             };
        public Form1()
        {
            InitializeComponent();

            this.dataGridView1.AutoGenerateColumns = false;
            this.ColumnName.DataPropertyName = "Name";
            this.ColumnPersonality.DataPropertyName = "PersonalityIdentifier";
            this.ColumnPersonality.DisplayMember = "Label";
            this.ColumnPersonality.ValueMember = "PersonalityCode";
            this.ColumnPersonality.DataSource = new List<Personality>()
                                                       {
                                                           // Create three types of personalities.
                                                           // (Display name, Internal identifier)
                                                           new Personality("Average", PersonalityCode.X),
                                                           new Personality("Average", PersonalityCode.Y),
                                                           new Personality("Average", PersonalityCode.Z)
                                                       };

            this.dataGridView1.DataSource = _peopleList;
        }

        void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            if (this.dataGridView1.CurrentCell.ColumnIndex == this.ColumnPersonality.Index)
            {
                int rowIndex = dataGridView1.CurrentRow.Index;
                var person = _peopleList[rowIndex];

                var comboBox = e.Control as DataGridViewComboBoxEditingControl;
                if (person.PersonalityIdentifier != null)
                    comboBox.SelectedValue = person.PersonalityIdentifier;
                else
                    comboBox.SelectedValue = string.Empty;

                comboBox.SelectionChangeCommitted -= this.comboBox_SelectionChangeCommitted;
                comboBox.SelectionChangeCommitted += this.comboBox_SelectionChangeCommitted;
            }
        }

Open in new window


other classes:
    class Person
    {
        public Person(string name, PersonalityCode personalityCode)
        {
            Name = name;
            PersonalityIdentifier = personalityCode;
        }

        public string Name { get; set; }
        public PersonalityCode PersonalityIdentifier { get; set; }
    }

    class PersonalityCode
    {
        public static PersonalityCode X { get { return new PersonalityCode(0); } }
        public static PersonalityCode Y { get { return new PersonalityCode(1); } }
        public static PersonalityCode Z { get { return new PersonalityCode(2); } }

        private int id;
        public PersonalityCode(int id)
        {
            this.id = id;
        }

        public override bool Equals(object obj)
        {     //  Removed for clarity...  }

        public override int GetHashCode()
        {    //  Removed for clarity...  }        
    }

    class Personality
    {
        public Personality(string label, PersonalityCode personalityCode)
        {
            Label = label;
            PersonalityCode = personalityCode;
        }

        public string Label { get; set; }
        public PersonalityCode PersonalityCode { get; set; }

        public override bool Equals(object obj)
        {  //  Removed for clarity...  }

        public override int GetHashCode()
        {  //  Removed for clarity...  }    
    }

Open in new window

0
Comment
Question by:dominicwong
  • 5
  • 3
8 Comments
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 40302650
Hi dominicwong;

According to Microsoft documentation:
DataGridViewComboBoxColumn does not support the use of multiple items with identical display values.
0
 

Author Comment

by:dominicwong
ID: 40302672
Thanks Fernando for your prompt response.

I looked at the link pointed by your comment. It referred to "DataGridViewComboBoxColumn.Items" property. Also same comment was said about "DataGridViewComboBoxCell.Items" property. But nothing similar was said about "DataGridViewComboBoxColumn.DataSource" which I am using.
Does the restriction apply to "DataGridViewComboBoxColumn" in general or only when the "Items" property was used?
0
 

Author Comment

by:dominicwong
ID: 40302677
I came across with the word "Calculated field" for displaying identical names in combobox. Is there any similar approach for "DataGridViewComboBoxColumn"?
0
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 

Author Comment

by:dominicwong
ID: 40302690
The way that "DataGridViewComboBoxColumn" was used in my example was I had separate values for "DisplayMember" and "ValueMember". Because the value in "ValueMember" is different albeit same value in "DisplayMember", I wonder if the restriction stated by Microsoft is valid in this case.
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 40303828
To your question, "Does the restriction apply to "DataGridViewComboBoxColumn" in general or only when the "Items" property was used?", Items is a collection of objects that are to be displayed in the drop down and so I would expect it to apply in general seeming it does not specifically state otherwise.  

 To your question, "I came across with the word "Calculated field" for displaying identical names in combobox. Is there any similar approach for "DataGridViewComboBoxColumn"?", can you please post a link to that information.

To your question, "The way that "DataGridViewComboBoxColumn" was used in my example was I had separate values for "DisplayMember" and "ValueMember". Because the value in "ValueMember" is different albeit same value in "DisplayMember", I wonder if the restriction stated by Microsoft is valid in this case.", I think that the issue is that when the combo box loses focus and then gets focus back it does a search of the text in the text box to the displayed member and stops the search on the first one it finds.
0
 

Author Comment

by:dominicwong
ID: 40305286
I tried to look for that particular link again but couldn't find it. But when I look into other articles that talked about calculated field,  they were talking about combining different fields together to form a unique field. Basically they are not trying to show the original value as is.

Thanks again Fernando for your help and explanations.
0
 

Author Closing Comment

by:dominicwong
ID: 40305288
Thank you. Much appreciated!!
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 40305737
Not a problem dominicwong, only glad to help.
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

828 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