Solved

Does DataGridViewComboBoxColumn support duplicated names??

Posted on 2014-09-03
8
266 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 62

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
 

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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 62

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 62

Expert Comment

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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Delivering innovative fully-managed cloud services for mission-critical applications requires expertise in multiple areas plus vision and commitment. Meet a few of the people behind the quality services of Concerto.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

919 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now