Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Does DataGridViewComboBoxColumn support duplicated names??

Posted on 2014-09-03
8
Medium Priority
?
295 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 64

Accepted Solution

by:
Fernando Soto earned 2000 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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 

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 64

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 64

Expert Comment

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

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

730 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