Solved

Does DataGridViewComboBoxColumn support duplicated names??

Posted on 2014-09-03
8
262 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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

759 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

19 Experts available now in Live!

Get 1:1 Help Now