Solved

Does DataGridViewComboBoxColumn support duplicated names??

Posted on 2014-09-03
8
285 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

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

Setting up LaraDock for Laravel

Learn how to set up LaraDock in a Laravel project - LaraDock gives us an easy way to run a Laravel application using Docker in a single command.

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
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…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

623 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