I have 2 types of datagrid combobox columns. One where the value member is an integer (ID) and another where the value member is a string(Shortname). The display member (Longname) in both are strings.
The one with the string value member works fine.
The one with the integer displays the value member (the integer) instead of the display member. The drop down list is correct and I can select one, but when i leave the column it diplays the integer ID instead.

DataGridViewComboBoxColumn dgvCBcol = new DataGridViewComboBoxColumn();

            dgvCBcol.Name = col.ColName;
            dgvCBcol.DataPropertyName = col.ColName;
            if (col.ColName == "DLQ_FREQ")
            {
                // this is the one with an integer
                List<clsItemsNumID> cmb = col.CmbItemsNumID as List<clsItemsNumID>;
                dgvCBcol.DataSource = cmb;
                dgvCBcol.ValueMember = "ID";
            }
            else
            {   // this is the one with a string 
                List<cmbAlias> cmb = col.CmbItems as List<cmbAlias>;
                dgvCBcol.DataSource = cmb;
                dgvCBcol.ValueMember = "ShortName";
            }
            dgvCBcol.Width = 100;
            dgvCBcol.DisplayMember = "LongName";
            dgvCBcol.HeaderText = col.ColHdrText;   
            dgvCBcol.SortMode = DataGridViewColumnSortMode.Automatic;
 
//********clsItemsNumID is as follows
public class clsItemsNumID
    {
        private string longName;
        private int id;
        public int ID
        {
            get { return id; }
            set { id = value; }
        }
        public string LongName
        {
            get { return longName; }
            set { longName = value; }
        }
        
        public clsItemsNumID(int ID, string LongName)
        {
            this.id = ID;
            this.longName = LongName;
        }

Recommended Answers

Where do you have defined "ShortName" property in the clsItemsNumID class? I dont see it.
Do it this way:

public class clsItemsNumID
    {
        public string LongName { get; set; }
        public string ShortName { get; set; }
        public int ID { get; set; }
        
        public clsItemsNumID(int _id, …
Jump to Post

All 4 Replies

Where do you have defined "ShortName" property in the clsItemsNumID class? I dont see it.
Do it this way:

public class clsItemsNumID
    {
        public string LongName { get; set; }
        public string ShortName { get; set; }
        public int ID { get; set; }
        
        public clsItemsNumID(int _id, string _long, string _short)
        {
            this.ID = _id;
            this.LongName = _long;
            this.ShortName = _short;
        }
     }

Now you can set the DisplayMember (LongName) and ValueMember (ones ID, other time ShortName).
It has to work.
Mitja

Shortname is in the List<cmbAlias> , which the string columns use and that works fine. I only showed the clsItemsNumID because that is where I have the problem.

I read on another post that had a similar issue and they recomend to convert the field in the datasource to an integer. How would do I do that?

After much pain, I found out what the problem is and I wanted to share it since I found that many other people had similar problems. The database column was setup as "NUMBER" (i am using oracle), so in the datasource list (public class clsItemsNumID above) the "ID" field needed to be "double", not "int".
If the column were NUMBER(6), then the "ID" field would have to be "decimal".

I am not sure why this column was setup that way - it is a lot of wasted space and not very descriptive. Personally, I would have preferred to use a char instead.

I am glad you did it. But this was your mistake, I couldnt know that your set the column to type of Number (decimal) in your dataBase.
bye
Mitja

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.